-
Notifications
You must be signed in to change notification settings - Fork 926
Spaces before colons in macro_rules? #2534
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
Comments
As much as I prefer to keep formatting consistent between function definitions and |
This was fixed by adding a space before the colon. But I wonder: should we add spaces before some other characters, too? Here it sort of looks like
But if we add a space, it's clearer that
I'd probably prefer to always add a space after the token type, unless it is followed by one of |
I'd probably rather not add spaces neither before nor after column (which seems what @topecongiro agreed with above but implemented differently?). That way rustfmt would be consistent with most of the existing macro code in the wild, including the formatting in the Rust Book that probably inspired everyone else. https://doc.rust-lang.org/book/first-edition/macros.html |
Reopening because the commit that closed this did not address where the discussion seemed to be heading -- that as much as it may be motivated by the formatting of function definitions, macro syntax has different requirements and we should not be inserting whitespace after the metavariable colon. I agree with @topecongiro's and @RReverser reasoning. macro_rules! foo {
($a:ident : $b:ty) => {};
($a:ident $b:ident $c:ident) => {};
} |
I always reformat macros as functions when I touch them in code (and use appropriate lower/upper-case for macro parameters), it makes them looking much more like normal code and therefore more readable for a non-macro-expert. I agree with @stjepang though that colons used as separators and not as a part of macro_rules! foo {
($a: ident : $T: ty) => {};
($a: ident $b: ident $c: ident) => {};
}
We should fix the book, IMO. |
@dtolnay The first PR adds a space before non-metavariable colon which comes after metavariable. The following PR (#2549) removes a space after metavariable colon. So the issue is fixed on the master. We have not settled on how we should format macro arguments. I think that we have three options to choose from:
Currently rustfmt follows 1. |
Thank you! That PR had not been linked to this issue before. Looking forward to a release. |
Related to #2753, in that we may want to punt on this formatting via a flag for now. |
Removing from milestone since we no longer format macro matchers by default (90c5792) |
So we can decide on the formatting later - there is no clear answer here and we're approaching the cut-off for 1.0, so I think we need to punt. |
I see. Too bad, I really wanted macros to be autoformatted (which is why I PRd it in the first place) but understandable that exact rules need more care and time. I will still be able to opt-in to their formatting with |
Oh wait, if I'm reading the diff correctly, this also affects just the matching definitions, but bodies will be still formatted? |
That's the defaults, yes. You'll be able to change both in rustfmt.toml |
Sounds perfect and reasonable, thanks. |
Closes rust-lang#2534 The behavior described in the original issue can no longer be reproduced. The tests show that to be the case regardless of if `format_macro_matchers` is `true` or `false`.
Before formatting:
After formatting:
This formatting makes the macro patterns difficult to read.
I understand why rustfmt adds a space after every
:
, and it makes a lot of sense when arguments are separated by commas or semicolons. But if arguments are separated by spaces or other kinds of delimiters, the end result is not great. :(cc @RReverser
The text was updated successfully, but these errors were encountered: