Skip to content

Clean up various things in the parser - #162269

Merged
rust-bors[bot] merged 9 commits into
rust-lang:mainfrom
fmease:parser-cleanups
Sep 8, 2026
Merged

Clean up various things in the parser#162269
rust-bors[bot] merged 9 commits into
rust-lang:mainfrom
fmease:parser-cleanups

Conversation

@fmease

@fmease fmease commented Sep 4, 2026

Copy link
Copy Markdown
Member

View all comments

I urge you to review this PR commit by commit. Note that almost every single commit comes with a rather elaborate commit message. Well, excuse the probably questionable grammar & structure of some of these commit messages, I was a bit tired while writing them; I'll go over them again some other day to revise them.

What does this PR actually do?

  1. removes parse error recovery code that's already obsolete / unreachable or that's rendered that way by other changes in this PR
  2. simplifies, generalizes & consolidates the parse error recovery code for outer attributes in invalid contexts1
  3. adds comments explaining some non-obvious parts of bare trait object type parsing

(No LLM was or will be used by me during the entire creation process of this PR)

Footnotes

  1. It doesn't consolidate all of them. I've left alone the one for if & else branches and the one for use-trees. Those offer slightly more complex recovery. I might revisit this in the future.

@fmease fmease added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Sep 4, 2026
@rustbot

This comment was marked as outdated.

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 4, 2026
@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

r? @mati865

rustbot has assigned @mati865.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 21 candidates

Comment thread tests/ui/generic-associated-types/parse/trait-path-missing-gen_arg.rs Outdated
Comment thread compiler/rustc_parse/src/parser/attr.rs Outdated
Comment thread tests/ui/parser/attribute-on-empty.rs
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

r? @petrochenkov
@rustbot author

@rustbot rustbot assigned petrochenkov and unassigned mati865 Sep 4, 2026
@rustbot rustbot added S-waiting-on-author Status: This is 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 Sep 4, 2026
Comment thread compiler/rustc_parse/src/parser/ty.rs
Comment thread compiler/rustc_parse/src/parser/ty.rs
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
Comment thread tests/ui/parser/attribute-on-empty.rs
Comment thread compiler/rustc_parse/src/parser/attr.rs Outdated
Comment thread tests/ui/generic-associated-types/parse/trait-path-missing-gen_arg.rs Outdated
Comment thread compiler/rustc_parse/src/parser/ty.rs Outdated
@rustbot

This comment has been minimized.

@fmease

fmease commented Sep 5, 2026

Copy link
Copy Markdown
Member Author
  1. Applied all suggestions.
  2. Dropped the "outer" from "outer attributes cannot be applied to generic arguments", so it now reads "attributes cannot be applied to generic arguments"
    • that's because I felt like the word "outer" falsely insinuated that inner attributes might be allowed
    • the wording "cannot be ascribed to $TARGET" already says enough

@fmease fmease added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 5, 2026
@fmease
fmease force-pushed the parser-cleanups branch 2 times, most recently from c31845c to e0b59b1 Compare September 6, 2026 12:51
@rust-bors

This comment has been minimized.

@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after rebase.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is 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 Sep 7, 2026
The comment was added in PR rust-lang#115131.
The feature was removed in PR rust-lang#131045.
The logic was added in commit ff61949
(2020) to suppress noisy follow-up syntax errors. For example, consider
`src/test/ui/issues/issue-39616.rs` from said commit.

However, removing it nowadays doesn't regress any diagnostics.

On the contrary, since we no longer try to recover from such malformed
array & slice types, the code in `parse_generic_arg` responsible for
attempting to recover from complex const exprs that weren't wrapped in
braces `{` `}` has an easier time to recover from e.g., `[10]` (as in
`Wrap<[10]>`) and doesn't need to create "dummy" diagnostics to attach
suggestions onto.

In the next commit, we'll remove the special casing of recovered array &
array types in `parse_generic_arg` that's no longer necessary.
See the previous commit for details.
@rustbot

rustbot commented Sep 7, 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.

Instead of checking for `#[` before trying to parse outer attributes,
check for `#` or doc comments instead. This way, we automatically recover
from leading doc comments and inner attributes on types, too.

Previously, we would only recover from doc comments and inner attributes
on types if they were preceded by normal outer attributes.

Move the whole recovery into a separate more general function in
preparation for using it in various other places, too.

This means we drop the recovery from attributes in type contexts that
aren't ascribed to a valid type (at least until the next sync point).
For example, `type T = #[a];` and `fn f() -> #[a] {}` are now fatal
(meaning the parser will only continue parsing from the next sync point).

This could be considered unfortunate but honestly it didn't make much sense:
Why would we only defatalize syntax errors in type contexts if there are
preceding attributes (which aren't legal / expected on types in the first
place, mind you); there's no reason to recover from e.g., `type T = #[a] @;`
(which used to lead to bad follow-up diagnostics). Defatalizing syntax
errors isn't inherently bad, it just needs to be done carefully and not
made dependent on "random" conditions.
In commit 0564168, the check for doc
comments was added to prevent us from trying to reparse the type as a
const expression and suggesting to wrap it in `{` `}` if successful as
that's unlikely what the user meant.

However, now that we always try to parse outer attributes (including
doc comments) on types to provide better diagnostics, this case in
`parse_generic_arg` can't be reached anymore.
Given we now recover from outer attributes on any type,
this specialized logic is no longer necessary.

Given we now recover from outer attributes on any type,
this specialized logic is no longer necessary.
Use our new routine. Again, this means we can now also recover from
leading doc comments and inner attributes.

Moreover, we now actually store type & const arguments that have outer
attributes in the AST instead of using `{type error}` & `{const error}`
for them, respectively.
Admittedly, this slightly regresses diagnostic quality in certain
scenarios but this can be improved in the future *for all* users of
`recover_from_outer_attributes`.

For example, it's a bit contradictory to say "expected outer doc comment"
followed by "doc comments cannot be applied to XY". We should probably
only emit a single error saying "doc comments cannot be applied to XY"
but that needs modifications to `parse_outer_attributes` or something
similar.
1. Rename `parse_ty_tuple_or_parens` to `parse_paren_start_ty` since it
   doesn't just parse parenthesized types and tuple types, it also parses
   bare trait object types where the first bound is parenthesized.
   Naming is inspired by `parse_path_start_ty`.
2. Don't use `parse_remaining_bounds_path` ("finish parsing bare trait
   object type") when recovering from `for<…> impl …` / `for<…> impl …`
   since we're in fact not parsing a bare trait object type but either an
   impl-Trait type or a dyn-Trait type. This means we no longer need to
   match on the `TyKind` to extract the `bounds`.
3. Rename `parse_remaining_bounds_path` to
   `finish_parsing_bare_trait_object_ty` since the previous name really
   didn't make it clear that it's specific to bare trait object types.
4. Add a bunch of comments explaining what we're doing in (now)
   `parse_paren_start_ty` to successfully parse a bare trait object type
   where the first bound is parenthesized.
5. Inline `parse_remaining_bounds` since it didn't really have any reason
   to exist. The callsites look just fine or even better with the inlined
   body.
@fmease

fmease commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

@bors r=petrochenkov

@rust-bors

rust-bors Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 8a845e5 has been approved by petrochenkov

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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 7, 2026
@rust-bors

This comment has been minimized.

@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 8, 2026
@rust-bors

rust-bors Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

☀️ Test successful - CI
Approved by: petrochenkov
Duration: 3h 9m 27s
Pushing 6b410a8 to main...

@github-actions

github-actions Bot commented Sep 8, 2026

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 2e6e39f (parent) -> 6b410a8 (this PR)

Test differences

Show 34 test diffs

Stage 1

  • [ui (polonius)] tests/ui/parser/attribute-on-type-or-gen-arg.rs: [missing] -> pass (J1)
  • [ui (polonius)] tests/ui/parser/attribute-on-type.rs: pass -> [missing] (J1)
  • [ui] tests/ui/parser/attribute-on-type-or-gen-arg.rs: [missing] -> pass (J2)
  • [ui] tests/ui/parser/attribute-on-type.rs: pass -> [missing] (J2)

Stage 2

  • [ui] tests/ui/parser/attribute-on-type-or-gen-arg.rs: [missing] -> pass (J0)
  • [ui] tests/ui/parser/attribute-on-type.rs: pass -> [missing] (J0)

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

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 6b410a8387fa41607d50b4f7da844f4d79a565bc --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. test-x86_64-mingw-1: 2h 8m -> 3h 6m (+45.4%)
  2. dist-riscv64-linux-gnu: 1h 8m -> 1h 33m (+36.1%)
  3. dist-x86_64-msvc-alt: 1h 58m -> 2h 41m (+35.6%)
  4. test-x86_64-gnu-parallel-frontend: 2h -> 1h 18m (-35.0%)
  5. dist-various-2: 40m 2s -> 53m 10s (+32.8%)
  6. test-x86_64-gnu-stable: 1h 57m -> 2h 30m (+28.2%)
  7. dist-powerpc64-linux-gnu: 1h 16m -> 1h 37m (+27.8%)
  8. dist-x86_64-netbsd: 1h 34m -> 1h 9m (-27.2%)
  9. test-arm-android: 1h 29m -> 1h 51m (+25.3%)
  10. test-tidy: 5m 14s -> 6m 31s (+24.5%)
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.

@fmease
fmease deleted the parser-cleanups branch September 8, 2026 04:03
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (6b410a8): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

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

Max RSS (memory usage)

Results (primary -0.1%)

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

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

Cycles

Results (primary -0.2%, secondary -2.3%)

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

mean range count
Regressions ❌
(primary)
0.5% [0.4%, 0.5%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.8% [-1.3%, -0.6%] 5
Improvements ✅
(secondary)
-2.3% [-2.4%, -2.2%] 2
All ❌✅ (primary) -0.2% [-1.3%, 0.5%] 9

Binary size

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

Bootstrap: 482.327s -> 477.191s (-1.06%)
Artifact size: 403.54 MiB -> 403.62 MiB (0.02%)

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) C-cleanup Category: PRs that clean code up or issues documenting cleanup. merged-by-bors This PR was explicitly merged by bors. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants