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

Braces are removed from single-item import in macro where they are required #6047

Open
junbl opened this issue Jan 28, 2024 · 2 comments
Open
Labels
a-macros bug Panic, non-idempotency, invalid code, etc.

Comments

@junbl
Copy link

junbl commented Jan 28, 2024

In a macro that takes a path as an argument:

macro_rules! get_msg {
    ($module:path) => {
        let message = {
            use $module::{MESSAGE}; // note the (usually unnecessary) brackets
            MESSAGE
        };
        println!("{}", message);
    };
}

The braces around MESSAGE are required, and the code fails to compile without them. However, rustfmt removes them.

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ca5ee49a37c35354f67d82d6303c7f5a

@ytmimi ytmimi added a-macros bug Panic, non-idempotency, invalid code, etc. labels Jan 28, 2024
@ytmimi
Copy link
Contributor

ytmimi commented Jan 28, 2024

@junbl thank you for the report. Confirming I can reproduce this with the latest source version of rustfmt 1.7.0-nightly (cedb7b50 2024-01-25).

Some workarounds until we address this:

  1. You can add a #[rustfmt::skip] attribute to prevent rustfmt from modifying the formatting:

    macro_rules! get_msg {
        ($module:path) => {
            #[rustfmt::skip]
            let message = {
                use $module::{MESSAGE}; // note the (usually unnecessary) brackets
                MESSAGE
            };
            println!("{}", message);
        };
    }
  2. If you're using unstable rustfmt features you can turn declarative macro body formatting off by setting format_macro_bodies=false.

Also linking the tracking issue for format_macro_bodies (#3355)

@ytmimi
Copy link
Contributor

ytmimi commented Jan 29, 2024

Oh, and I'm not sure if this is an option for you, but if the rust playground link shows how you'll actually call the macro (with a single identifier, like get_msg!(x)), then you could possibly rewrite the macro as follows and the code will still compile after rustfmt removes the braces {}:

macro_rules! get_msg {
-    ($module:path) => {
+    ($module:ident) => {
        // this works but the braces in the import get lost on format
        let message = {
            use $module::{MESSAGE};
            MESSAGE
        };

        // this would be nicer but doesn't work
        // let message = $module::MESSAGE;

        println!("{}", message);
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-macros bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

No branches or pull requests

2 participants