knurling-rs / defmt

Efficient, deferred formatting for logging on embedded systems
https://defmt.ferrous-systems.com/
Apache License 2.0
750 stars 69 forks source link

Defmt's proc macros issue an error if the filename is not a correct identifier #853

Open gregtwice opened 3 weeks ago

gregtwice commented 3 weeks ago

I have a file name 00_Hello_world in my bin folder which contains the following code:

#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};

use defmt::*;

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let p = embassy_stm32::init(Default::default());

    let mut led = Output::new(p.PA5, Level::High, Speed::Low);

    loop {
        info!("{}", 2);
        led.set_high();
        Timer::after_millis(300).await;

        // defmt::info!("low");
        led.set_low();
        Timer::after_millis(300).await;
    }
}

When compiling I get the following error:

error: `{input}` is not a valid identifier
   --> src\bin\00_Hello_world.rs:18:9
    |
18  |         info!("{}", 2);
    |         ^^^^^^^^^^^^^^ in this macro invocation
    |
   ::: C:\Users\gregtwice\.cargo\registry\src\index.crates.io-6f17d22bba15001f\defmt-macros-0.3.9\src/lib.rs:137:1
    |
137 | #[proc_macro_error]
    | ------------------- in this expansion of `info!`

It compiles fine when removing the "00_" part of the file, or when removing the macros. So I guess that the issue is due to the fact that my filename is not a correct identifier. Do you think the error message could be improved? Or is it outside the scope of the crate?

Thank you for your consideration.

Urhengulas commented 3 weeks ago

The error stems from https://github.com/knurling-rs/defmt/blob/75fb317dab468a9fc786fe1f1da421b6b24e64d2/macros/src/function_like/log/env_filter/parse.rs#L102-L105

Urhengulas commented 3 weeks ago

It compiles fine when removing the "00_" part of the file, or when removing the macros. So I guess that the issue is due to the fact that my filename is not a correct identifier. Do you think the error message could be improved? Or is it outside the scope of the crate?

Thank you for your consideration.

I believe we should allow using filenames that are not correct identifiers.

PS. also the error message should be better. panic!("`{}` is not a valid identifier", input) improves it.