Skip to content

Rustfmt subtree update (1.11.0) - #163142

Merged
rust-bors[bot] merged 72 commits into
rust-lang:mainfrom
ytmimi:rustfmt-subtree-update
Sep 22, 2026
Merged

rust-bors[bot] merged 72 commits into
rust-lang:mainfrom
ytmimi:rustfmt-subtree-update

Conversation

@ytmimi

@ytmimi ytmimi commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

jieyouxu and others added 30 commits August 24, 2026 17:07
Even when locally, to minimize supply-chain risk from very-new registry
artifacts.
Link to the same policy followed by rust-lang/rust.
This also follows what rust-lang/rust is doing.
Follow the same LLM policy as rust-lang/rust
See <rust-lang/rustfmt#6639>. Compared to the
reported MCVE, the test case added:

- Adds another layer of outermost block doc comment for the outer
  module.
- Also exercises inner block doc comments (the reported example involves
  only outer block doc comments).
# The symptom

Consider the example

```rs
pub mod outer {
    /**First comment.
*/
    pub struct Inner {
        pub octets: Vec<u8>,
    }
}
```

Previously, this was *non-idempotent* and required **2** passes to
converge to a final formatting.

## Format 1

Format pass 1 tries to realign the block doc comment closer with the
starter `/**`. Notice that the closer `*/` aligns with `/**` without a
leading whitespace in the closer.

```diff
 pub mod outer {
     /**First comment.
-*/
+    */
     pub struct Inner {
         pub octets: Vec<u8>,
     }
```

After this pass, if you run `rustfmt --check` on this, rustfmt will
report formatting difference:

```text
Diff in /Users/joe.xu/Documents/repos/rustfmt/foo.rs:1:
 pub mod outer {
     /**First comment.
-    */
+     */
     pub struct Inner {
         pub octets: Vec<u8>,
     }
```

## Format 2

Run `rustfmt` again, then formatting converges to

```rs
pub mod outer {
    /**First comment.
     */
    pub struct Inner {
        pub octets: Vec<u8>,
    }
}
```

Notice that the closer `*/`'s `*` now has a leading whitespace, aligning
it with the first `*` in the starter `/**`.

# Analysis

What went wrong? The formatting pathways hit here are fairly localized,
and mostly concern the pathway `identify_comment ->
light_rewrite_comment`.

The `light_rewrite_comment` helper has a bug where the implementation
doesn't agree with the indent expressed by the comment:

```text
// This is basically just l.trim(), but in the case that a line starts
// with `*` we want to leave one space before it, so it aligns with the
// `*` in `/*`.
```

Take the same `/**First comment.\n*/` example, the execution trace
broadly looks like:

```
 rustfmt_nightly::comment::identify_comment{}
  TRACE rustfmt_nightly::comment style=DoubleBullet
  TRACE rustfmt_nightly::comment block comment
  TRACE rustfmt_nightly::comment has_bare_lines=false, first_group_ending=20
  TRACE rustfmt_nightly::comment first_group="/**First comment.\n*/", rest=""
  TRACE rustfmt_nightly::comment !normalize_comments && !wrap_comments && !(is_doc_comment && format_code_in_doc_comments)
   rustfmt_nightly::comment::light_rewrite_comment{orig="/**First comment.\n*/", offset=Indent { block_indent: 4, alignment: 0 }, is_doc_comment=true}
    TRACE rustfmt_nightly::comment first_non_whitespace=0
    TRACE rustfmt_nightly::comment left_trimmed="/**First comment."
    TRACE rustfmt_nightly::comment first_non_whitespace=0
    TRACE rustfmt_nightly::comment left_trimmed="*/"
```

The previous implementation was

```rs
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
let left_trimmed = if let Some(fnw) = first_non_whitespace {
    if l.as_bytes()[fnw] == b'*' && fnw > 0 {
        &l[fnw - 1..]
    } else {
        &l[fnw..]
    }
} else {
    ""
};
```

The problem was that, when the closer line has no leading whitespace,
i.e. `l = "*/"`, `first_non_whitespace` would trigger on `*`, and so
`fnw = 0`. This would hit the else branch `&l[fnw..]`, which eventually
produces an closer alignment without a leading whitespace:

```rs
    /**
    */  <- missing leading whitespace
```

# Fix

The key logical fix here is to drop the `fnw > 0` condition, and in the
`*`-leading branch, pad a whitespace and directly use the
leading-whitespace trimmed portion `&l[fnw..]`.

I used a `Cow` here because the leading-whitespace padding would need a
`format!` allocation, whereas the other branch need only a string slice
and no additional allocation. *Maybe* it's not worth the extra
complexity and we should just use `String`, but yeah.
For better or worse, for outer block doc comments, this patch does
unfortunately change the comment formatting for cases like

```
$ cat foo.rs
/**
*/
mod foo {}
```

Stable rustfmt considers this already well-formatted.

```bash
$ rustfmt +stable --version
rustfmt 1.9.0-stable (8bab26f 2026-07-14)
```

```bash
$ rustfmt +stable foo.rs --check --config-path=/dev/null
```

With this patch, we only consider the closer well-formatted if its
asterisk `*` is aligned with the first `*` in the opener.

```
$ rustfmt-dev foo.rs --check --config-path=/dev/null
Diff in /Users/joe.xu/Documents/repos/rustfmt/foo.rs:1:
 /**
-*/
+ */
 mod foo {}
```

I believe this is acceptable, since:

1. Comments are explicitly carved out from stable formatting stability
   guarantees, and
2. This impacts block doc comments, which IME is extremely rarely used.
The difference manifests in:

(1) The closer's asterisk `*` now alignments with starter asterisk.

Previously there was a `rustc_mir_transform` block doc comment that had
a misaligned closer:

```rs
/**
 * Integrator.
 *
 * Integrates blocks from the callee function into the calling function.
 * Updates block indices, references to locals and other control flow
 * stuff.
*/            <--
```

This is now properly aligned with the starter.

(2) Continuing asterisks now align with starter asterisk.

Previously there was a `rustc_codegen_llvm` block doc comment

```rs
/*
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
*
[...]
*
*/
```

where continuing asterisks were flush with line beginnings and not
aligned with starter asterisk. Continuing asteriks now align with the
starter asterisk.
Before when we failed to parse an item we'd hit an infinite loop because we
wouldn't advance the parser. Now we make sure to advance the parser before
starting the next loop iteration.
fix: Prevent infinite loops when parsing items from `cfg_select!` arms
test: Add more #![feature(generic_const_items)] test cases
* Replace `ref` patterns with `&` ones
* Use `Box::deref` to simplify matching log
* Use name fields, since the role of the fields wasn't immediately clear
  in a tuple
* Split out a helper function to make things slightly more explicit: the
  old callsite `Self::is_tup_field_access(expr)` made a redundant check
  that a `Field` expression (already determined from the `match`
  condition) is indeed a `Field` expression.
…potent-block-doc-comment

Fix non-idempotent block doc comment closer rewrite
@ytmimi ytmimi added the subtree-sync PR updates a subtree (miri, clippy, etc). Ignored by no-merges check label Sep 22, 2026
@rustbot

rustbot commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

rustfmt is developed in its own repository. If possible, consider making this change to rust-lang/rustfmt instead.

cc @rust-lang/rustfmt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Sep 22, 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.

Thanks, commit history looks right to me. You can r=me once PR CI is green.

View changes since this review

@ytmimi ytmimi changed the title Rustfmt subtree update Rustfmt subtree update (1.11.0) Sep 22, 2026
@ytmimi

ytmimi commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

@bors r=jieyouxu p=1 rollup=never

@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 51aa65e has been approved by jieyouxu

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 22, 2026
@rust-bors

This comment has been minimized.

jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 22, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 22, 2026
@rust-bors rust-bors Bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 22, 2026
@rust-bors

rust-bors Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: jieyouxu
Duration: 3h 4m 32s
Pushing e5b9509 to main...

@rust-bors
rust-bors Bot merged commit e5b9509 into rust-lang:main Sep 22, 2026
14 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor
What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing c4a0daa (parent) -> e5b9509 (this PR)

Test differences

Show 10 test diffs

10 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard e5b95097d9a14bdec7cd9101dde67ee3aad2578a --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-i686-linux: 1h 12m -> 1h 53m (+55.9%)
  2. test-x86_64-gnu-llvm-22-1: 1h 13m -> 44m 4s (-39.7%)
  3. dist-android: 23m 17s -> 31m 58s (+37.3%)
  4. test-various: 2h 12m -> 1h 23m (-37.2%)
  5. test-x86_64-gnu-llvm-22-3: 1h 58m -> 1h 15m (-36.5%)
  6. test-x86_64-gnu-stdlib-semver-check: 16m 21s -> 10m 27s (-36.1%)
  7. dist-x86_64-solaris: 1h 53m -> 1h 12m (-36.0%)
  8. test-x86_64-mingw-1: 2h 20m -> 3h 3m (+30.1%)
  9. test-armhf-gnu: 1h 36m -> 1h 8m (-29.6%)
  10. test-i686-msvc: 2h 39m -> 1h 54m (-28.6%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (e5b9509): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.8% [0.8%, 0.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 3.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.3% [2.8%, 3.9%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.3% [2.8%, 3.9%] 2

Cycles

Results (primary -3.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.9% [2.9%, 2.9%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-9.8% [-9.8%, -9.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.4% [-9.8%, 2.9%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 487.214s -> 487.336s (0.03%)
Artifact size: 406.37 MiB -> 406.32 MiB (-0.01%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. subtree-sync PR updates a subtree (miri, clippy, etc). Ignored by no-merges check T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.