Closed commonkestrel closed 11 months ago
@rustbot label +O-AVR
Using LTO doesn't seem to be required anymore (i.e. it works in all cases) - checked using:
#![no_std]
#![no_main]
use core::fmt;
use core::fmt::Write;
use panic_halt as _;
use ufmt::uWrite;
use void::Void;
struct Serial<'a>(&'a mut dyn uWrite<Error = Void>);
impl fmt::Write for Serial<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let tx = core::hint::black_box(&mut self.0);
for c in s.chars() {
_ = tx.write_char(c);
}
Ok(())
}
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
let mut serial = Serial(&mut serial);
_ = serial.write_fmt(format_args!("hello world\n"));
_ = serial.write_fmt(format_args!("{}\n", 3));
loop {
//
}
}
... and simavr.
I tried this code:
I expected the output:
Instead, the addition of the second
write_fmt
or the addition of anunwrap()
crashes the program after compilation and upload but before the program executes. After a bit of testing, I found that addinglto = true
under[profile.dev]
in Cargo.toml fixes the issue.Something that might be helpful is that when
unwrap()
is added towrite_str
, instead of printing the given string, it prints a piece of what seems like a compiler error, which matches the length of the supplied string, shown here:args.len()C:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\fmt\mod.rsunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed 'isize::MAX'called 'Option::unwrap()' on a 'None' valueC:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\char\convert.rsC:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\str\iter.rsC:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\str\validations.rsErrorattempt to add with overflowC:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\iter\traits\accum.rsunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed 'isize::MAX'attempt to add with overflowunsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed 'isize::MAX'C:\Users\User\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rust
Meta
rustc --version --verbose
:Custom target
avr-atmega328p.json
: