Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gregtwice opened this issue Jun 11, 2024 · 5 comments

Comments

@gregtwice
Copy link

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
Copy link
Member

The error stems from

fn validate_identifier(input: &str) {
syn::parse_str::<Ident>(input)
.unwrap_or_else(|_| panic!("`{input}` is not a valid identifier"));
}

@Urhengulas
Copy link
Member

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.

@lure23
Copy link

lure23 commented Aug 9, 2024

Bit by this as well, on examples/1-ranging-basic.rs etc.

Would appreciate the limitation to be lifted if there's no technical reason for its existance.

@Urhengulas
Copy link
Member

@lure23 @gregtwice I gave it a shot in https://github.com/knurling-rs/defmt/tree/accept-crate-names-with-leading-numbers, but could not get it to work fully. Feel free to pick up the work.

@lure23
Copy link

lure23 commented Aug 14, 2024

Unfortunately I’m not confident enough yet, in Rust, but thanks for the link. Prefixing with _ serves me well for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants