Skip to content

Fix statement comment wrapping - #7170

Merged
ytmimi merged 2 commits into
rust-lang:mainfrom
matthewhughes934:comments-issues-7167
Oct 13, 2026
Merged

ytmimi merged 2 commits into
rust-lang:mainfrom
matthewhughes934:comments-issues-7167

Conversation

@matthewhughes934

Copy link
Copy Markdown
Contributor
  • Rename line_start to comment_prefix

    This naming was a bit confusing: I would expect a 'line start' of a
    comment to include any leading indent etc., e.g. for the comments in:

      // top level comment
    
      fn f() {
          // function comment
          ...
      }
    

    I would expect:

    • line_start: // , and
    • line_start: //

    Rather than both to be // .

  • Fix comments not taking full width when combined

    By excluding the width of the indent when combining comments, since
    last_line_width includes this. Otherwise e.g. when formatting:

      V------V (indent)    (end of line) ---V
              // some comment with final word
              // new line fits word
    

    We'd would compute the offset using:

      last_line_width = "        // word".len()`
    

    (i.e. indent+comment prefix+content), then subtract only the width of
    the comment prefix (and " ") and incorrectly conclude that won't fit
    if combined with the following comment.

    References: rustfmt gives legal length comments a ragged edge in argument lists #7167

  • I did not use an LLM to create a change in this PR.

  • I used an LLM to create a change in this PR, and I have explained below how it was used.

This naming was a bit confusing: I would expect a 'line start' of a
comment to include any leading indent etc., e.g. for the comments in:

    // top level comment

    fn f() {
        // function comment
        ...
    }

I would expect:

* `line_start`: `// `, and
* `line_start`: `    // `

Rather than both to be `// `.
By excluding the width of the indent when combining comments, since
`last_line_width` includes this. Otherwise e.g. when formatting:

    V------V (indent)    (end of line) ---V
            // some comment with final word
            // new line fits word

We'd would compute the offset using:

    last_line_width = "        // word".len()`

(i.e. indent+comment prefix+content), then subtract only the width of
the comment prefix (and `" "`) and incorrectly conclude that won't fit
if combined with the following comment.

References: rust-lang#7167
@rustbot rustbot added the S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. label Oct 9, 2026
@matthewhughes934
matthewhughes934 marked this pull request as ready for review October 10, 2026 08:10
@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 Oct 10, 2026
@matthewhughes934 matthewhughes934 changed the title Comments issues 7167 Fix statement comment wrapping Oct 10, 2026
@jieyouxu jieyouxu self-assigned this Oct 12, 2026
@jieyouxu jieyouxu added UO-wrap_comments Unstable option: wrap_comments UO-comment_width Unstable option: comment_width X-impacts-unstable-options Expected formatting impact: only affects code formatted by unstable options labels Oct 12, 2026
Comment thread src/comment.rs Outdated
Comment on lines +886 to +887
- self.comment_prefix.len();
- self.comment_prefix.len()
- self.fmt_indent.width();

@jieyouxu jieyouxu Oct 13, 2026 •

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.

Could we use saturing subtraction here? I suspect some kind of weird issue like tabs spaces can cause us to underflow here

View changes since the review

Comment on lines +5 to +6
// Note: off-by-one bug: the 100 width wide line should not be formatted
// https://github.com/rust-lang/rustfmt/issues/7167

@jieyouxu jieyouxu Oct 13, 2026 •

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.

Maybe just,

// FIXME(#7167): off-by-one bug: the 100 width wide line should not be formatted

View changes since the review

@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.

Thanks, just #7170 (comment), feel free to merge otherwise.

EDIT: maybe hold and rebase after #7165 since that is extremely conflict-prone.

View changes since this review

@ytmimi

ytmimi commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@matthewhughes934 thanks for working on this and @jieyouxu thanks for reviewing. I should have time to update #7165 later today, but I think I need to rebase that anyway so let's get this one merged first.

@ytmimi
ytmimi added this pull request to the merge queue Oct 13, 2026
Merged via the queue into rust-lang:main with commit dda10e3 Oct 13, 2026
33 checks passed
@rustbot rustbot added release-notes Needs an associated changelog entry and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-notes Needs an associated changelog entry UO-comment_width Unstable option: comment_width 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.

4 participants