Repository navigation
Fix comments rewritten too long - #6802
Conversation
cecee09 to
9b9090f
Compare
9b9090f to
fc0c610
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
This seems largely reasonable to me. Some nits
@rustbot author
|
|
||
| let comment_width = ::std::cmp::min( | ||
| self.config.comment_width(), | ||
| self.config.comment_width() - self.block_indent.width(), |
There was a problem hiding this comment.
Question: can block_indent ever be > comment_width? 🤔
There was a problem hiding this comment.
Question: can
block_indentever be> comment_width? 🤔
Yes it can, and previously this would panic. I've pushed changes to the second commit and used Shape::comment directly instead (which does a saturating sub). I also updated the test to cover this case, see the indentation means this comment starts after 'comment_width' comment in the added test.
There was a problem hiding this comment.
Suggestion: can you include a comment (heh) in this test re. what is being demonstrated specifically? I.e. comment_width versus max_width?
There was a problem hiding this comment.
Suggestion: can you include a comment (heh) in this test re. what is being demonstrated specifically? I.e.
comment_widthversusmax_width?
See latest changes the second commit, I've tried to make it clear exactly what we're testing/expecting to see.
|
Reminder, once the PR becomes ready for a review, use |
fc0c610 to
6988476
Compare
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
There was a problem hiding this comment.
Let's not modify the content of any existing test cases.
If you want to add your changes in addition to what's there that seems fine to me, but I don't want to remove the fn main() { ... } .
| // pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus. | ||
| // In hac habitasse platea dictumst. Vivamus a orci at nulla tristique | ||
| // condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit | ||
| // amet, ipsum. In pharetra sagittis nunc. | ||
| // pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor | ||
| // faucibus. In hac habitasse platea dictumst. Vivamus a orci at nulla | ||
| // tristique condimentum. Donec arcu quam, dictum accumsan, convallis | ||
| // accumsan, cursus sit amet, ipsum. In pharetra sagittis nunc. |
There was a problem hiding this comment.
Is this wrapping too soon?
This file is configured with
// rustfmt-style_edition: 2024
// rustfmt-wrap_comments: true
// rustfmt-hard_tabs: true
>>> len("// pulvinar lacus orci eget nibh. Maecenas sed nibh non lacus tempor faucibus.")
79Pushing faucibis to the following line would make it
>>> len("// faucibus. In hac habitasse platea dictumst. Vivamus a orci at nulla tristique")And there's
>>> len("// condimentum. Donec arcu quam, dictum accumsan, convallis accumsan, cursus sit")
80With the default tab_spaces=4 those are al over the comment_width default of 80
There was a problem hiding this comment.
Same as above. Let's keep fn main() { ... }.
There was a problem hiding this comment.
Same as above. Let's keep
fn main() { ... }.
Done: first commit now is just adding the new example
| self.config.max_width() - self.block_indent.width(), | ||
| ); | ||
| let comment_shape = Shape::legacy(comment_width, comment_indent); | ||
| let comment_shape = Shape::indented(comment_indent, self.config).comment(self.config); |
There was a problem hiding this comment.
Could you help me understand what calling .comment() here changes?
There was a problem hiding this comment.
Could you help me understand what calling
.comment()here changes?
it means we consider the indentation + comment width when determining the width of a line.
Previously, the cmp::min would result in the minimum of:
self.config.max_width() - self.block_indent.width()i.e. content width - indent, orself.config.comment_width()i.e. just the comment width
So if we took self.config.comment_width() as the width, and you had a line that was 4 indent + 80 comment characters, it would be considered to be 80 characters wide, so e.g. it would fit in the default comment_width even through it was 84 characters wide
There was a problem hiding this comment.
Thanks for explaining that. Sometimes the Shape code can be confusing 😅
There was a problem hiding this comment.
Sometimes the Shape code can be confusing
Good to know I'm not the only one that finds if confusing 😅
When formatting 'missed' spans (I'm still not sure I understand exactly
what 'missed' means).
Previously, if `comment_width` was less than `max_width -
$current_indent` we would format the comment ignoring the length of any
indentation, so e.g. the following, with `comment_width=38` would not be
formatted
fn foo() {
// The '.' is at 'comment_width' . A
()
}
This involved rewriting some existing tests, without impacting what
they're meant to be asserting on. This emphasises that this patch _is
not_ backwards compatible: while I believe the change to be correct
(some tests had comments longer than `comment_width` that weren't being
formatted). So there's a trade-off to be made between correctness and
stability, since `comment_width` is still not stabilised this may be
acceptable.
Issue: rust-lang#6801
6988476 to
e325046
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This changed in rust-lang/rustfmt#6802
The daily format check using the Rust nightly toolchain began returning errors starting with the run at 21:00 UTC on August 30. The newer nightly includes [rustfmt #6802][1], which changed comment wrapping behavior. [1]: rust-lang/rustfmt#6802
`comment_width = 80` counts the whole line, indentation included, which wraps far more aggressively than intended; the latest nightly rustfmt (rust-lang/rustfmt#6802) started enforcing it on comments it previously left alone. Turn `wrap_comments` off rather than reformat everything. `max_width = 100`, `comment_width = 80` and `use_small_heuristics = "Default"` are rustfmt's defaults, so drop them too; only the two nightly-only import options remain.
The daily format check using the Rust nightly toolchain began returning errors starting with the run at 21:00 UTC on August 30. The newer nightly includes [rustfmt #6802][1], which changed comment wrapping behavior. [1]: rust-lang/rustfmt#6802 (cherry picked from commit 7f5fc4c)
Reformat with a current nightly rustfmt (>= 2026-08-20) that includes upstream fix rust-lang/rustfmt#6802 ("Fix comments rewritten too long"). Previously, comment_width wrapping ignored the current indentation level, so deeply indented comments wrapped too close to max_width (100) instead of the intended comment_width (80). This commit only reflows existing comment text produced by rustfmt; no code, string literals, or doc-test examples were touched.
With rust-lang/rustfmt#6802, `rustfmt` updates many comments in the code. Thi patch is the result of `rustfmt **/*.rs`.
Simplify test case for issue 5023
By replacing the code quoted from the issue with a single comment that
more directly exposes the issue. This is to
when rewriting lines (my main motivation for this change, since it
makes the followup patch simpler)
This was tested by partially reverting
368a9b7 with the patch:
The re-running tests, or executing
rustmftdirectly on this test,triggered the panic from the original bug.
Make comment wrapping indent aware
When formatting 'missed' spans (I'm still not sure I understand exactly
what 'missed' means).
Previously, if
comment_widthwas less thanmax_width - $current_indentwe would format the comment ignoring the length of anyindentation, so e.g. the following, with
comment_width=38would not beformatted
This involved rewriting some existing tests, without impacting what
they're meant to be asserting on. This emphasises that this patch is
not backwards compatible: while I believe the change to be correct
(some tests had comments longer than
comment_widththat weren't beingformatted). So there's a trade-off to be made between correctness and
stability, since
comment_widthis still not stabilised this may beacceptable.
Closes: #6801