-
Notifications
You must be signed in to change notification settings - Fork 931
Consistent format of struct post-comments in new line - with and without trailing comma #5607
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
base: master
Are you sure you want to change the base?
Conversation
Oops! I don't know how I missed #4655 .... Usually I don't try to resolve issues with open PRs. Regarding #4768, I did see it, but I am not sure it is the right approach. As I mentioned in the description, it may be that #4757 fixed by this PR is actually a non-issue. Regarding #4655, since it was for R2, I tried now to back-port it, to compare its output to my PR output. I am not sure that the back-port is good enough as
struct Foo {
bar: (), // Comment
bazbaz: (), // Comment
} is formatted as: struct Foo {
bar: (), // Comment
bazbaz: (), // Comment
} Instead of remaining as is.
struct Foo {
bar: ()
,
// Comment
}
struct Baz {
bar: ()
, // Comment
} is formatted as: struct Foo {
bar: (),
}
struct Baz {
bar: (),
} |
Still haven't had a chance to look at this, but these changes will need to be version gated to prevent breaking our backwards compatibility guarantees. You can likely revert all the changes made to existing tests once the version gate is added. |
Just to make sure, before I add the version gate. As I wrote in the description, there will be no formatting change to code that was already formatted. The changes to the test cases are because the source code is non-formatted. Therefore, backward compatibility is required only if the assumption is that users are expecting that for new (or not formatted) code the related new lines will be ignored. Is this the case? |
I tried to add the version gating, but I don't find an easy way to pass Is there another way to deduce the |
Got it! Thanks for looking into this. Given that existing Let me know if that's something you've already tried and if we need to brainstorm another approach. |
…omments in new line - with and without trailing comma
f3c6c56
to
8c863a9
Compare
Added the version gating by adding the The main change was to add |
Hey @davidBar-On I just noticed that #4768 was a previous attempt at solving this issue. It's currently targeting the |
Per PR #4768 test cases, it combines comment that start a new line with the previous line. I.e. the code: match 1 {
_ => {}
// hello!
}
match 2 {
_ => {},
// hello!
} is formatted as: match 1 {
_ => {} // hello!
}
match 2 {
_ => {} // hello!
} This is fixing the issue described in #4757, but my understanding is that a comment that starts a new line should not be combined with the previous line, i.e. that #4757 is actually a non-issue. For both #4654 and #4757 I took the approach that comments that start a new line will remain in a new line. Therefore my PR output output of the code above is: match 1 {
_ => {}
// hello!
}
match 2 {
_ => {}
// hello!
} |
Thanks for taking a look! Just double checking, but if comments are written on the same line to begin with your PR will force them onto the next line, right? or am I misunderstanding something? Given the following input: match 1 {
_ => {} // hello!
}
match 2 {
_ => {}, // hello!
} Will your PR produce this output? match 1 {
_ => {}
// hello!
}
match 2 {
_ => {}
// hello!
} |
Lines on the same line remain on the same line. For the given code, the PR produce this output: match 1 {
_ => {} // hello!
}
match 2 {
_ => {} // hello! |
Fixes #4654
Resolves #4757
Suggested resolution to the inconsistency of formatting struct post-comment that are in new line, depending on whether there is a trailing comma after the struct item. Without this change, this code:
Is formatted as:
The suggested change is that the post-comment will always start in a new line, so the output of both structs will be the same. As far as I understand, keeping the new line is the general approach of rustfmt.
Note that this change contradicts some of the existing test cases (that I changed). However, there should not be any backward compatibility issue. This is because already formatted code should not change. The change suggested in the PR should affect only code that was not formatted before.
The suggested approach practically makes #4757 a non-issue, because if it is accepted that comments in a new line should remain in a new line, than the "Expected output" of the issue is wrong.