Skip to content

Fix comments rewritten too long - #6802

Merged
ytmimi merged 2 commits into
rust-lang:mainfrom
matthewhughes934:fix-comments-rewritten-too-long
Aug 20, 2026
Merged

ytmimi merged 2 commits into
rust-lang:mainfrom
matthewhughes934:fix-comments-rewritten-too-long

Conversation

@matthewhughes934

@matthewhughes934 matthewhughes934 commented Feb 13, 2026 •

Copy link
Copy Markdown
Contributor
  • 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

    1. Make the underlying issue easier to reason about
    2. Make this test not behave upon the behaviour of indentation length
      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:

    diff --git i/src/string.rs w/src/string.rs
    index 3b971188..561e70ac 100644
    --- i/src/string.rs
    +++ w/src/string.rs
    @@ -278,9 +278,6 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
             }
             cur_index
         };
    -    if max_width_index_in_input == 0 {
    -        return SnippetState::EndOfInput(input.concat());
    -    }
    
         // Find the position in input for breaking the string
         if line_end.is_empty()
    

    The re-running tests, or executing rustmft directly 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_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.

Closes: #6801

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Feb 13, 2026
@matthewhughes934
matthewhughes934 force-pushed the fix-comments-rewritten-too-long branch from cecee09 to 9b9090f Compare February 13, 2026 21:15
@jieyouxu jieyouxu added X-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options UO-wrap_comments Unstable option: wrap_comments and removed X-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options labels Feb 18, 2026
@matthewhughes934
matthewhughes934 force-pushed the fix-comments-rewritten-too-long branch from 9b9090f to fc0c610 Compare February 25, 2026 19:01
@rustbot

This comment has been minimized.

@jieyouxu jieyouxu self-assigned this May 30, 2026

@jieyouxu jieyouxu left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems largely reasonable to me. Some nits
@rustbot author

View changes since this review

Comment thread src/missed_spans.rs Outdated

let comment_width = ::std::cmp::min(
self.config.comment_width(),
self.config.comment_width() - self.block_indent.width(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: can block_indent ever be > comment_width? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: can block_indent ever 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can you include a comment (heh) in this test re. what is being demonstrated specifically? I.e. comment_width versus max_width?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can you include a comment (heh) in this test re. what is being demonstrated specifically? I.e. comment_width versus max_width?

See latest changes the second commit, I've tried to make it clear exactly what we're testing/expecting to see.

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Jun 8, 2026
@rustbot

rustbot commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@matthewhughes934
matthewhughes934 force-pushed the fix-comments-rewritten-too-long branch from fc0c610 to 6988476 Compare August 16, 2026 18:08
@rustbot

This comment has been minimized.

@matthewhughes934

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels Aug 16, 2026

@ytmimi ytmimi left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert those test simplification changes. We almost never modify existing test cases. I'm fine with adding additional things to the test case though.

Also had one question for you regarding the change in missed_spans.

View changes since this review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() { ... } .

Comment on lines -21 to +24
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this wrapping too soon?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.")
79

Pushing 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")
80

With the default tab_spaces=4 those are al over the comment_width default of 80

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Let's keep fn main() { ... }.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Let's keep fn main() { ... }.

Done: first commit now is just adding the new example

Comment thread src/missed_spans.rs
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you help me understand what calling .comment() here changes?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, or
  • self.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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explaining that. Sometimes the Shape code can be confusing 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes the Shape code can be confusing

Good to know I'm not the only one that finds if confusing 😅

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 19, 2026
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
@matthewhughes934
matthewhughes934 force-pushed the fix-comments-rewritten-too-long branch from 6988476 to e325046 Compare August 20, 2026 20:46
@rustbot

rustbot commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

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.

@ytmimi ytmimi left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for answering my questions + applying feedback. We're good here.

View changes since this review

@ytmimi
ytmimi added this pull request to the merge queue Aug 20, 2026
Merged via the queue into rust-lang:main with commit 701c02e Aug 20, 2026
27 checks passed
@rustbot rustbot added the release-notes Needs an associated changelog entry label Aug 20, 2026
@rustbot rustbot removed the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Aug 20, 2026
flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Sep 1, 2026
noritada added a commit to noritada/grib-rs that referenced this pull request Sep 2, 2026
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
sandhose added a commit to element-hq/matrix-authentication-service that referenced this pull request Sep 3, 2026
`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.
noritada added a commit to noritada/grib-rs that referenced this pull request Sep 6, 2026
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)
ustorbeck added a commit to network-analytics/NetCalyx that referenced this pull request Sep 7, 2026
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.
Hywan added a commit to Hywan/matrix-rust-sdk that referenced this pull request Sep 8, 2026
With rust-lang/rustfmt#6802, `rustfmt` updates
many comments in the code. Thi patch is the result of `rustfmt **/*.rs`.
@ytmimi ytmimi added this to the 1.100.0 milestone Sep 24, 2026
@ytmimi ytmimi removed the release-notes Needs an associated changelog entry label Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

UO-wrap_comments Unstable option: wrap_comments X-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrap_comments partially ignores comment_width with bullet lists

4 participants