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>
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
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
Fixesrust-lang/rust#161213
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
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.
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.
Closesrust-lang/rust#149246
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.
rustc_parse: suggest removing semicolon before `if` block
Closesrust-lang/rust#159812
Suggest removing semicolon before the block in an `if` expression
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
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.
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.
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.
This allows implementing EIIs that don't have a default impl without the
involvement of symbol aliases by simply changing the mangled symbol name
of the implementation, which would make them trivially compatible with
all backends and targets. This change will be left to a followup PR.
Improve diagnostic for patterns in function pointer types
In preparation for some work for https://github.com/rust-lang/rust/issues/158499
Diagnostic before:
```
error[E0642]: patterns aren't allowed in methods without bodies
--> $DIR/fn-ptr-pattern.rs:4:14
|
LL | pat2: fn(1..3: bool),
| ^^^^
|
help: give this argument a name or use an underscore to ignore it
|
LL - pat2: fn(1..3: bool),
LL + pat2: fn(_: bool),
|
```
Diagnostic after:
```
error[E0642]: patterns aren't allowed in function pointer types
--> $DIR/fn-ptr-pattern.rs:4:14
|
LL | pat2: fn(1..3: bool),
| ^^^^
|
help: give this argument a name or use an underscore to ignore it
|
LL - pat2: fn(1..3: bool),
LL + pat2: fn(_: bool),
|
```
Bubble bad path error while parsing field to avoid unecessary second error
Noticed while looking at rust-lang/rust#92685. Add test for the case in that ticket.
Writing the type of each element inside a tuple pattern like
`let (a: bool, b: u8) = ...` isn't valid, but the current error doesn't
really tell you that. It just says "expected one of `)`, `,`, `@`, `if`,
or `|`, found `:`" and leaves you to work it out.
A let pattern can be followed by a type, so we can suggest the fix: move
the types out into a tuple type after the pattern, so `(a: bool, b: u8)`
becomes `(a, b): (bool, u8)`. Elements without a type just get `_`.
Parsing then keeps going with the types dropped, so you only get the one
error.
Try to recover less from incorrectly parsed const arg
The error recovery can cause a cascade of irrelevant errors, so instead return the `PResult::Err` and let the parser fail further up in the logic.
Fixrust-lang/rust#88416.
Split function parsing out of `item.rs` to a new module.
`item.rs` is approaching the tidy-enforced length limit, and this will make room for new additions.
I have split this change out of PR rust-lang/rust#159044 to reduce its complexity (and because this large code move quickly becomes stale).
Avoid stale closure recovery state across statements
Fixesrust-lang/rust#159989
`current_closure` was cleared when starting a new expression through `parse_expr`, but statement expressions call `parse_expr_res` directly. This allowed closure recovery state from one statement to remain active while parsing the next statement.
This pr clears `current_closure` when entering `parse_stmt_without_recovery`, matching the behavior of `parse_expr`.
da2697d1d8/compiler/rustc_parse/src/parser/expr.rs (L55-L56)da2697d1d8/compiler/rustc_parse/src/parser/expr.rs (L63-L64)
```
error: expected one of `.`, `?`, `]`, or an operator, found `,`
--> $DIR/do-not-suggest-semicolon-between-macro-without-exclamation-mark-and-array.rs:2:19
|
LL | let _x = vec[1, 2, 3];
| ^ expected one of `.`, `?`, `]`, or an operator
|
help: you might have meant to call a macro
|
LL | let _x = vec![1, 2, 3];
| +
```
Make some parser structured suggestions verbose and tweak their wording
Replace most of the `.span_suggestion(` in `rustc_parse` with `.span_suggestion_verbose(`, as they are more readabale, if more verbose. Verbose suggestions also tend to highlight off-by-one `Span` errors better.
Tweak some of the touched diagnostics to bring them more in-line with our house style.
CC https://github.com/rust-lang/rust/issues/141973