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

rustfmt makes it impossible to explain attributes with comments in the same line #4573

Closed
RalfJung opened this issue Dec 2, 2020 · 5 comments · Fixed by #5325
Closed

rustfmt makes it impossible to explain attributes with comments in the same line #4573

RalfJung opened this issue Dec 2, 2020 · 5 comments · Fixed by #5325

Comments

@RalfJung
Copy link
Member

RalfJung commented Dec 2, 2020

Input

Sometimes an attribute needs a comment explaining why it exists:

#[cfg(not(miri))] // Miri does not deduplicate consts (https://github.com/rust-lang/miri/issues/131)

Output

Unfortunately rustfmt insists on putting the comment on a separate line (and it moves it after the attribute, which is even worse):

#[cfg(not(miri))]
// Miri does not deduplicate consts (https://github.com/rust-lang/miri/issues/131)

Expected output

Just like I can have comments to the right of code, explaining what the code does, I expect to have comments to the right of attributes, explaining what the attributes do:

#[cfg(not(miri))] // Miri does not deduplicate consts (https://github.com/rust-lang/miri/issues/131)

Meta

  • rustfmt version: whatever ./x.py fmt does in rustc
@jyn514
Copy link
Member

jyn514 commented Dec 16, 2020

rustfmt version: whatever ./x.py fmt does in rustc

This is currently nightly-2020-11-19 (you can tell by looking at src/stage0.txt).

@RalfJung
Copy link
Member Author

This looks very similar to #3255 -- in both cases rustfmt changes comment-after-code to move the comment onto its own line, thus changing the semantic meaning of the comment. IMO rustfmt should never do this.

@ytmimi
Copy link
Contributor

ytmimi commented Apr 12, 2022

I'm unable to reproduce this on rustfmt 1.4.38-nightly (2d9bc460 2022-04-07). Also It's not totally clear to me which PR resolved this. @calebcartwright any recollection of a PR that adjusted how missing comment spans were joined? without digging to deeply into it I'd assume that's where the fix was. I'll open a PR with tests cases for this.

@calebcartwright
Copy link
Member

I don't recall anything offhand, though it is getting a bit late in the day. Could you make sure you're testing locally with the same configuration settings that are used in the rust-lang/rust repo?

@calebcartwright
Copy link
Member

Closed as this does appear to be sorted, but please let us know if you see a recurrence @RalfJung

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

Successfully merging a pull request may close this issue.

5 participants