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 · 7 comments
Labels
good first issue Good for newcomers

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.

@Urhengulas Urhengulas added the good first issue Good for newcomers label Oct 18, 2024
@Urhengulas
Copy link
Member

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

Probably that would be enough to fix it, just internally prefixing it with an underscore.

@Urhengulas
Copy link
Member

It would be good to know (and document!) why the path segments even need to be valid identifier. Maybe we can even lift it?

Briefly looking in the env_filter implementation (https://github.com/rust-cli/env_logger/blob/main/crates/env_filter/src/parser.rs#L58) it seems like they do not place this restriction upon their path segments. But our context is a bit different since our filter runs during compiletime.

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

No branches or pull requests

3 participants