-
Notifications
You must be signed in to change notification settings - Fork 900
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
Formatting struct with trailing comment is not idempotent #4848
Comments
I'm also not sure whether moving the comment to the line above is the intended formatting behavior. I would have expected standalone comments to be kept on their own line, but perhaps this is intentionally not the case for comments within structs? |
You are correct. Idempotency is a core tenant of rustfmt and non-idempotent formatting behavior is indeed a bug. For some reason I feel like this is a duplicate but will keep it open unless I find a previous report.
tl;dr comments are hard 😄 rustfmt is an AST-driven/pretty-printer style formatter, where comments are not explicitly represented within the tree so rustfmt has to peek back inside the source to recover comments and determine where to place them within the reformatted version of code. However, it can often be ambiguous which node the developer intended the comment to be associated to. In the case of a comment that follows an element in a list like this (fields within a struct) the absence of a comma between the end of the element and start of a comma has always been interpreted as a trailing comment which works well except in some cases where the trailing comment is after the last element, as in your examples. That behavior has been reported previously (refs #4654) and probably isn't too terribly far away from resolution, although the associated PR stalled out. |
Hey @calebcartwright I've been looking into this and found that the issue sort of has to do with whether you're using Maybe that's obvious based on the example, but I think the point helps us better understand the issue so I'm mentioning it. From my understanding, the source of the bug can be tracked to line 615 in Lines 607 to 628 in 365a2f8
Given the first struct: struct Foo {
field1: usize,
field2: usize,
// TODO: maybe we add more fields below
} the struct Foo {
field1: usize,
field2: usize
// TODO: maybe we add more fields below
} Running rustfmt again with struct Foo {
field1: usize,
field2: usize // TODO: maybe we add more fields below
} Right now I'd like some guidance on what the right behavior should be. Do we preserve newlines or not? I quickly tested a solution where newlines were preserved and one where newlines were always removed and it looks like either way tests are going to break. looks like #4655 also ran into issues of tests breaking when trying to fix this. |
Going to close this in favor of #4654 |
I am working with with
trailing_comma = "Never"
set in arustfmt.toml
config, running the nightly rustfmt, and find the following idempotency issue. My understanding is that instead of the following behavior, rustfmt should fully format the code once and not change on future executions.Starting with the code:
Calling rustfmt the first time gives
Calling rustfmt the second time gives
All subsequent calls leave it in this final state.
My rustfmt version is
rustfmt 1.4.37-nightly (8cf990c 2021-05-15)
The text was updated successfully, but these errors were encountered: