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

Regression (poor-formatting) in macros formatting since 1.50 #5419

Closed
maxwase opened this issue Jun 29, 2022 · 5 comments
Closed

Regression (poor-formatting) in macros formatting since 1.50 #5419

maxwase opened this issue Jun 29, 2022 · 5 comments

Comments

@maxwase
Copy link

maxwase commented Jun 29, 2022

Since 1.50 the following macros:

macro_rules! format_me_wrong {
    ($prefix:ty |$arg1:pat, $arg2:pat_param| $code:expr) => {};
}

formats like this:

fn main() {
    format_me_wrong!(
        bool | _,
        _ | {
            true;
            true;
        }
    );
}

instead of this:

fn main() {
    format_me_wrong!(
        bool | _,  _ | {
            true;
            true;
        }
    );
}

The key part is to have a literal before "closure" and 2+ lines after "closure"

@ytmimi
Copy link
Contributor

ytmimi commented Jun 29, 2022

Thanks for the report.

I'm not entirely sure that 1.50 introduced this change. I just checked using rustfmt 1.4.38-stable (fe5b13d6 2022-05-18) and experienced the same formatting using the snippet

fn main() {
    format_me_wrong!(
        bool | _,  _ | {
            true;
            true;
        }
    );
}

Also checked rustfmt 1.5.1-nightly (2f3ddd9f 2022-06-27) and got the same formatting.

@maxwase
Copy link
Author

maxwase commented Jun 29, 2022

That is kinda strange, that's my results

 src git:(master) ✗ cargo +nightly fmt --version
rustfmt 1.5.1-nightly (8308806 2022-06-28)
 src git:(master) ✗ cargo +nightly fmt --check  
Diff in /home/maksim/dev/my/expr/src/main.rs at line 4:
 
 fn main() {
     format_me_wrong!(
-        bool | _,  _ | {
+        bool | _,
+        _ | {
             true;
             true;
         }
Diff in /home/maksim/dev/my/expr/src/main.rs at line 11:
     );
 }
 src git:(master) ✗ cargo +1.49 fmt -- --check
 src git:(master) ✗ cargo +1.49 fmt --version  
rustfmt 1.4.25-stable (0f29ff6 2020-11-11)

@calebcartwright
Copy link
Member

calebcartwright commented Jun 29, 2022

I'd seen this but hadn't had a chance to respond yet. My assumption was that @maxwase was referring to the Rust 1.50 release and the version of rustfmt that shipped with it, not the recent rustfmt version from a couple weeks ago (rustfmt is versioned independently of the compiler).

I also suspect that this is not a regression at all, but is simply reflective of the limits of formatting args to macro calls, and that if one were to go back to rust 1.49 you'd discover rustfmt actually isn't formatting the call at all (which can be tested by modifying the input to be highly misformatted which should be left as-is by rustfmt).

For context, rustfmt is a formatter of Rust syntax. Args to macro calls aren't always valid Rust syntax strictly speaking. In order to support the desire of our users for rustfmt to provide some formatting for calls, rustfmt will attempt to see if args can be parsed as valid rust syntax, and if they can, then rustfmt applies the corresponding rules. In some cases, args to calls can actually be parsed as valid syntax, but that's not actually what was intended.

In this case, those args can be parsed as binary expressions and are formatted as such. The change you see from 1.49 to 1.50 is simply due to the compiler's parsing being enhanced to be able to handle that sequence of tokens in a way that allow for successful parsing-as-an-expression, so rustfmt 1.4.25/rust 1.50 was the first version where rustfmt was actually able to format that call. This isn't a regression, and isn't going to change.

As such if you'd like different formatting your options are to change the call delimiters to {} or to skip the formatting of the call(s) altogether

@calebcartwright calebcartwright closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2022
@maxwase
Copy link
Author

maxwase commented Jun 29, 2022

As such if you'd like different formatting your options are to change the call delimiters to {} or to skip the formatting of the call(s) altogether

Oh, thanks!

@calebcartwright
Copy link
Member

I should also add that if you don't want to change the delimiters, then the only way to currently skip would be via attributes applied on each call (https://github.com/rust-lang/rustfmt#tips).

However, we will also be adding a new configuration option in an upcoming release (#5347) which will allow you to designate macro calls to be skipped for formatting from one place in your config file

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