Repository navigation
Rustfmt subtree update (1.11.0) - #163142
Conversation
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.
…ed test file for 2024 style-edition
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
Closes rust-lang/rustfmt 7092
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.
Refactor some `chain` code
…potent-block-doc-comment Fix non-idempotent block doc comment closer rewrite
Through triagebot.
Adjust triagebot label config
…26-09-21 subtree-push nightly-2026-09-21
Prepare for v1.11.0 release
|
These commits modify the If this was unintentional then you should revert the changes before this PR is merged.
cc @rust-lang/rustfmt |
|
@bors r=jieyouxu p=1 rollup=never |
This comment has been minimized.
This comment has been minimized.
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 differencesShow 10 test diffs10 doctest diffs were found. These are ignored, as they are noisy. Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard e5b95097d9a14bdec7cd9101dde67ee3aad2578a --output-dir test-dashboardAnd then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
|
Finished benchmarking commit (e5b9509): comparison URL. Overall result: ❌ regressions - no action needed@rustbot label: -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
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.
CyclesResults (primary -3.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 487.214s -> 487.336s (0.03%) |
r? @jieyouxu