Commit Graph

2980 Commits

Author SHA1 Message Date
León Orell Valerian Liehr
8a845e5d15 Clarify the code responsible for parsing bare trait object types
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.
2026-09-07 21:43:51 +02:00
León Orell Valerian Liehr
4c139efaa5 Simplify recovery from outer attributes on cfg_select branches
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.
2026-09-07 21:43:51 +02:00
León Orell Valerian Liehr
3d5b7aaa01 Simplify recovery from outer attributes on generic args
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.
2026-09-07 21:43:51 +02:00
León Orell Valerian Liehr
712a16edb1 Remove specialized recovery from outer attributes on parameter types
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.
2026-09-07 21:43:51 +02:00
León Orell Valerian Liehr
9875dda210 Remove unreachable condition from recovery code
In commit 0564168895, 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.
2026-09-07 21:43:51 +02:00
León Orell Valerian Liehr
e92a69499d Simplify recovery from outer attributes on types
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.
2026-09-07 21:43:45 +02:00
León Orell Valerian Liehr
d82b08755b Remove obsolete recovery code from parse_generic_arg
See the previous commit for details.
2026-09-07 21:39:19 +02:00
León Orell Valerian Liehr
8af341adc3 Remove obsolete recovery logic for array & slice types
The logic was added in commit ff61949860
(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.
2026-09-07 21:39:19 +02:00
Jonathan Brouwer
25134621b1 Rollup merge of #162246 - bb1yd:fix-incorrect-meta-span, r=nnethercote
Fix incorrect meta span

Fixes rust-lang/rust#161472
We should use `attr_item.span` which covers the entire `MetaItem`.
2026-09-04 22:29:13 +02:00
khyperia
3b87c57eeb abby test DSL: AliasTyOutlivesViaEnv 2026-09-04 14:29:19 +02:00
b1yd
be0994f8f8 fix-incorrect-meta-span 2026-09-03 23:00:09 +08:00
León Orell Valerian Liehr
7a71a7440f Add warnings to can_begin_* methods that affect stable macro matching behavior 2026-09-02 11:25:49 +02:00
León Orell Valerian Liehr
c81640d058 Remove parse recovery for box expressions 2026-09-02 11:21:12 +02:00
León Orell Valerian Liehr
7ee0fba091 Render the box pattern removal diagnostic more actionable 2026-09-02 11:21:11 +02:00
Jonathan Brouwer
dd9c329207 Rollup merge of #161792 - fmease:fix-reserved-prefixes-lint-diags, r=mejrs
Fix and improve diagnostics for lint `rust_2021_prefixes_incompatible_syntax`

On main, when encountering token sequences in Rust <2021 that would get interpreted as...

1. ...C string literals in Rust >=2021,
   1. we report "*prefix `c` is unknown*" / "*prefix `cr` is unknown*" despite them obviously being known
   2. we claim that they "*[are] a hard error in Rust 2021*"
2. ...raw lifetimes in Rust >=2021 like `'r#a`, we suggest splitting them *after* the hash (so `'r#` and `a`) which obviously doesn't fix the issue; they need to be split *before* the hash (so `'r` and `#a`)
3. ...unknown (reserved) prefixes in Rust >=2021, we report "*prefix […] is unknown*" but to be pedantic the sequence is *not* a prefix in the current edition, it's just a normal identifier; to be correct & precise we should thus say that they *will* be parsed as a prefix (in Rust >=2021)

Lastly, unify / streamline all diagnostics pertaining to "reserved prefixes" (identifier/literal prefixes, hash prefixes (aka guarded string prefixes / multihashes))

---

Best reviewed commit by commit.

> [!NOTE]
> **Unblocks** PR https://github.com/rust-lang/rust/pull/161775.

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
2026-08-30 21:48:05 +02:00
León Orell Valerian Liehr
3fca755fdf Fix the suggestion span used for splitting raw lifetimes apart 2026-08-30 19:29:58 +02:00
León Orell Valerian Liehr
08f3b0e884 Unify and improve lint diagnostics for reserved prefixes 2026-08-30 19:29:56 +02:00
Jonathan Brouwer
3a93a22480 Rollup merge of #161794 - fmease:fix-weak-pin, r=adwinwhite
Fix handling of weak keyword `pin`

Part of https://github.com/rust-lang/rust/issues/130494.

On main, we use `is_ident_named` to detect weak keyword `pin` which is incorrect since it doesn't account for whether the identifier in question is raw (`r#`) or not. Namely, `r#pin` should *never* be considered (yes, even if it's followed by the disambiguator `const` or `mut`). As it stands, (1) we accept more code than we should (2) we're prone to rightfully triggering internal assertions thus ICE'ing.

Just use `is_keyword`&co which do those checks (indeed, it's also used for weak keywords). Furthermore, move `Symbol` `sym::pin` to `kw::Pin` and officially mark it as weak. This doesn't have any semantic effect, it's still a `Symbol`, but it's more conventional.

For reference, on main we ICE with `assertion failed: self.eat_keyword(expr!(Pin))` on inputs like the following:

* `type X = &r#pin const ();` (type)
* `fn scope() { let _ = &r#pin mut (); }` (expr)
* `fn scope() { let &r#pin const (); }` (pat)

Moreover, on main we wrongfully accept code like `trait Trait { fn f(&r#pin mut self); }` (assuming the feature is enabled).

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
2026-08-29 13:28:13 +02:00
Jonathan Brouwer
0ae3fd93a3 Rollup merge of #161796 - fmease:rm-dead-underscore-recovery, r=mu001999
Remove dead parse error recovery (underscores in expressions)

Back in b7f7756566 (2019) the parser was extended to recover from `_`s as elements of tuple expressions (this was later extended to also cover call expressions). At the time, `_` obviously wasn't an expression yet.

Nowadays `parse_expr_res` will *always* successfully parse `_` as an `ExprKind::Underscore` (irrespective of the passed `Restrictions`). Consequently, we will never reach the special case in `parse_expr_catch_underscore`. Drop this entire wrapper function.

<sub>(No LLM was or will be used by me during the entire creation process of this PR)</sub>
2026-08-26 08:01:00 +02:00
León Orell Valerian Liehr
8de399ac7b Remove dead parse error recovery (underscores in expressions) 2026-08-26 04:48:42 +02:00
León Orell Valerian Liehr
37c65a1fc4 Fix handling of weak keyword pin 2026-08-26 03:56:33 +02:00
Jonathan Brouwer
dda473214e Rollup merge of #161443 - khyperia:abbys-tests, r=BoxyUwU
add internal DSL for testing binders

implements https://github.com/rust-lang/project-assumptions-on-binders/issues/8

apologies to the inevitable swarm of people who get pinged for this...

basically the only interesting diff is `compiler/rustc_hir_analysis/src/check/wfcheck.rs`, which actually implements the test. there's probably still features we want to add to this DSL (e.g. `RegionConstraint::AliasTyOutlivesViaEnv`), and there might be some bugs lurking, but this is at least a base to work off of. It's perma-unstable and for internal use only, so support and quality doesn't have to be incredibly high. For example, parser recovery and whatnot is just, bad, but whatever.

r? BoxyUwU
2026-08-25 18:00:01 +02:00
Jacob Pratt
9f6bef7e24 Rollup merge of #161411 - im-lunex:fix_#161213, r=nnethercote
avoid overlapping const suggestions

Avoid emitting the optional `const-extraction` suggestion when its **component spans** overlap, this avoids the compiler panic while still emitting the normal error and keeping the suggestion for cases where it can be
generated correctly

Fixes rust-lang/rust#161213
2026-08-25 04:53:49 -04:00
cyrgani
9caab2652e remove box_patterns feature 2026-08-25 07:50:03 +00:00
Jonathan Brouwer
4410eba1c2 Rollup merge of #161637 - nnethercote:clarify-token-cursor, r=Kobzol
Clarify token cursor behaviour

The meaning of `TokenTreeCursor::index` is context-dependent: in the innermost (current) `TokenTreeCursor` it points to the next token tree, but in all the other (stack) `TokenTreeCursor`s it points to the current token tree. This makes the meanings of "current", "next", and "look_ahead" confusing for it and for `TokenCursor`.

This commit clarifies things by adjusting the stack `TokenTreeCursor`s to also point to the next token tree, and by improving various comments.

The commit also renames `TokenCursor::next` as
`TokenCursor::next_and_bump` for consistency with everything else: `next` means "get the next thing" and `bump` means "advance the cursor", and this operation does both.

r? @Kobzol
2026-08-24 15:06:59 +02:00
khyperia
210fd61779 add internal DSL for testing binders 2026-08-24 08:54:48 +02:00
Stuart Cook
9799322d5c Rollup merge of #155254 - scrabsha:push-qlmmuuwqnoqv, r=fmease
Recover on attribute in use tree
2026-08-24 14:21:48 +10:00
Nicholas Nethercote
1bc25c317b Clarify token cursor behaviour
The meaning of `TokenTreeCursor::index` is context-dependent: in the
innermost (current) `TokenTreeCursor` it points to the next token tree,
but in all the other (stack) `TokenTreeCursor`s it points to the current
token tree. This makes the meanings of "current", "next", and
"look_ahead" confusing for it and for `TokenCursor`.

This commit clarifies things by adjusting the stack `TokenTreeCursor`s
to also point to the next token tree, and by improving various comments.

The commit also renames `TokenCursor::next` as
`TokenCursor::next_and_bump` for consistency with everything else:
`next` means "get the next thing" and `bump` means "advance the cursor",
and this operation does both.
2026-08-24 14:07:37 +10:00
León Orell Valerian Liehr
1333ad790a Small tweaks to parse error recovery code 2026-08-23 14:39:15 +02:00
Sasha Pourcelot
e6e1f048cb Recover on attribute in use tree 2026-08-23 14:39:15 +02:00
Jacob Pratt
4e637b3c68 Rollup merge of #160279 - ArneshBanerjee:recover-tuple-type-ascription, r=fmease
Recover from tuple patterns with inline element types

If you write the element types inside a tuple pattern:

```rust
let (a: bool, b: u8) = (true, 1);
```

the error you get doesn't really explain the problem:

```
error: expected one of `)`, `,`, `@`, `if`, or `|`, found `:`
 --> src/main.rs:2:11
  |
2 |     let (a: bool, b: u8) = (true, 1);
  |           ^ expected one of `)`, `,`, `@`, `if`, or `|`
```

A let pattern can have a type after it, so now we point out what to do instead:

```
help: to annotate the types of a tuple's elements, write them as a tuple type after the pattern
  |
2 -     let (a: bool, b: u8) = (true, 1);
2 +     let (a, b): (bool, u8) = (true, 1);
  |
```

If an element doesn't have a type written, it just gets `_`, so `(a: bool, b)`
turns into `(a, b): (bool, _)`.

I kept this to let bindings only. That's the case where the pattern doesn't need
a type of its own, so once the types move into the suggestion the rest of the
statement parses fine and you get a single error instead of a pile of follow-up
ones. Match arms, nested patterns and function parameters are left alone.

Closes rust-lang/rust#149246
2026-08-23 05:59:33 -04:00
Arnesh Banerjee
aad8a5a7a3 Address review: recover in all pattern locations, drop extra allocation 2026-08-23 07:07:46 +05:30
Zalathar
83a13f64a6 Clean up a few _inner functions after ensure_sufficient_stack
After the removal of `ensure_sufficient_stack`, these inner and outer functions
no longer need to be separate.
2026-08-22 18:06:33 +10:00
mejrs
7cd9c920cd rustc_session; remove rustc_lint_defs rex-exports 2026-08-20 23:02:49 +02:00
im-lunex
08ec41ea3d avoid overlapping const suggestions 2026-08-20 22:45:47 +06:00
bors
eaa160c49d Auto merge of #161082 - nnethercote:overhaul-CoroutineMarker, r=mejrs
Overhaul `CoroutineKind`

Its structure is a bit weird. Details in individual commits.

r? @mejrs
2026-08-17 02:41:22 +00:00
Nicholas Nethercote
613e826eaa Overhaul CoroutineKind
It is an enum with three variant, and all the variants have identical
fields, which is silly. This commit does the following.

- Renames it as `CoroutineMarker`, because the `Kind` suffix is used for
  enums. (The existing doc comment already uses the word "marker".)

- Renames the existing `GenBlockKind` as `CoroutineKind`, uses it
  within `CoroutineMarker`, and adds `CoroutineKind::is_gen`.

This removes the need for the `span`, `closure_id` and `return_id`
methods; direct field access now suffices.
2026-08-17 12:29:59 +10:00
Makai
9126d55662 cleanup: rip out unnecessary iter().last() and iter().next() 2026-08-15 00:25:23 +08:00
Jacob Pratt
87f5f9b9e5 Rollup merge of #160861 - ravlynd:if-semi-before-block-suggestion, r=chenyukang
rustc_parse: suggest removing semicolon before `if` block

Closes rust-lang/rust#159812

Suggest removing semicolon before the block in an `if` expression
2026-08-12 20:17:32 -04:00
ravlyn
349eac6d92 rustc_parse: suggest removing semicolon before if block 2026-08-12 21:11:19 +07:00
Jonathan Brouwer
03c3d00ee3 Make inputs to ParenthesizedArgs a ThinVec<Param> in the compiler 2026-08-11 20:47:40 +02:00
bors
8a2fbe3ea8 Auto merge of #160830 - jhpratt:rollup-IMvXhX3, r=jhpratt
Rollup of 11 pull requests

Successful merges:

 - rust-lang/rust#160675 (bootstrap: Remove `PATH_REMAP` from command-line selector handling )
 - rust-lang/rust#160807 (Update rustc crate rkyv to 0.8.18)
 - rust-lang/rust#159690 (Clarify `--remap-path-scope` impact on `rustc` metadata)
 - rust-lang/rust#160560 (Add nightly-only support for Cargo unremap trim-paths files in `rust-gdb`)
 - rust-lang/rust#160608 (normalization rework: clean up projection_ty_core)
 - rust-lang/rust#160785 (Get rid of LLM disclosure checkboxes)
 - rust-lang/rust#160804 (Change .expect message on net/parser to follow precondition style)
 - rust-lang/rust#160805 (`extern "custom"`: add tests)
 - rust-lang/rust#160816 (Mark const ptr methods and free functions as inline(always) to match *mut)
 - rust-lang/rust#160820 (Stabilize fs_set_times)
 - rust-lang/rust#160826 (Rename parse_delimited_token_tree in cfg_select)
2026-08-10 06:25:17 +00:00
Yukang
41e53faaf0 Rename parse_delimited_token_tree to parse_cfg_select_branch_rhs 2026-08-10 09:40:01 +08:00
Chris Denton
4641990929 Remove ensure_sufficient_stack 2026-08-09 19:58:29 +00:00
Jonathan Brouwer
c10876b8a1 Rollup merge of #160744 - mejrs:struct-diagnostics-privacy, r=JonathanBrouwer
make more diagnostic structs pub(crate)
2026-08-09 18:22:27 +02:00
Jacob Pratt
3820cc943d Rollup merge of #158517 - JonathanBrouwer:named_fn_trait_parameters, r=JohnTitor
Initial implementation of named `Fn` trait parameters

This is an initial implementation of the feature `named_fn_trait_parameters`.
This implementation is incomplete (and also marked as such) , and needs significant further changes to be implemented correctly. To keep the size of this PR managable, I propose we do this in follow-up PRs.
I've added a test that shows the current behaviour, with `FIXME` comments where the test output is currently incorrect.

Tracking issue: https://github.com/rust-lang/rust/issues/158499
Accepted RFC: https://github.com/rust-lang/rfcs/pull/3955
2026-08-08 23:51:55 -04:00
Arnesh Banerjee
0790e9d398 Address review: drop parser snapshot, keep path-separator suggestion
Rework the recovery to happen in a single parse instead of parsing the
tuple pattern, resetting on failure, and parsing again. The element
closure now eats an optional `: <ty>` directly, so there's no parser
snapshot on the happy path.

Only treat the colon as a type annotation when it is followed by
whitespace, so `(m:C,)` keeps the existing "maybe write a path separator
here" suggestion for `m::C` and only `(m: C,)` gets the tuple-type
suggestion.
2026-08-08 19:30:37 +05:30
mejrs
d8ecbe7c01 Move ExprParenthesesNeeded diagnostic struct out of rustc_session
It was never used in that crate, so rustc_parse is the next obvious place to go. It's also used by rustc_hir_typeck, but sharing diagnostics between crates makes it easy for such things to become dead, so duplicate it.
2026-08-08 12:13:25 +02:00
Jacob Pratt
3c50d990c9 Rollup merge of #160303 - petrochenkov:ambattr3, r=chenyukang
rustc_parse: A few cleanups to expression parsing next to attributes

- The `_with` suffix is removed from `parse_expr_assoc_with` and `parse_expr_assoc_rest_with`. There's no version with `with`, the precedence is always specified.
- `parse_expr_res` and `parse_expr_assoc` are split into two versions taking pre-parsed attributes and not taking pre-parsed attributes. The latter versions are more common and can have simpler return type.
- "Dead" `parse_outer_attributes` is removed from `parse_expr_prefix_range`, it can never parse anything because the current token is a range separator.
- `bump` is moved out of `parse_expr_prefix_common`, it allows reusing `parse_expr_prefix_common` in one more place.
- A test covering some previously uncovered attribute parsing cases is added.
2026-08-08 03:10:08 -04:00
Jonathan Brouwer
8355006dd1 Rollup merge of #160592 - Diacod-I:if-let-chain-unclosed-delim-hint, r=estebank
Suggest if-let chain continuation on unclosed delimiter

Meant to fix rust-lang/rust#160550. When an unclosed brace-delimited block begins with && or ||, the "unclosed delimiter" error now points at that line, suggesting the user may have meant to continue an if-let chain.

- Records the leading `&&`/`||` span during lexing (in lex_token_tree_open_delim) and surfaces it in report_suspicious_mismatch_block.
- Adds a `tests/ui/parser` regression test.
2026-08-06 12:41:03 +02:00