Detect and provide suggestion for &raw EXPR#139392
Conversation
|
r? @davidtwco rustbot has assigned @davidtwco. Use |
| self.error_remove_borrow_lifetime(span, lt.ident.span.until(expr.span)); | ||
| } | ||
|
|
||
| // Add expected tokens if we parsed `&raw` as an expression. |
There was a problem hiding this comment.
🤔 do we know anything about the parser grammar such that we could eagerly recover here (if self.may_recover(), of course), at least in some cases?
I don't think we ever expect an identifier token to follow another expr in valid rust, so if we see &raw IDENT, we could actually do recovery here rather than just failing later on in parsing.
| // guides recovery in case we write `&raw expr`. | ||
| if borrow_kind == ast::BorrowKind::Ref | ||
| && mutbl == ast::Mutability::Not | ||
| && matches!(&expr.kind, ExprKind::Path(None, p) if p.is_ident(kw::Raw)) |
There was a problem hiding this comment.
(minor) is_ident or this matches! needs to account for raw identifiers, so we can exclude r#raw (raw raw) here
There was a problem hiding this comment.
This is a parsed ident, not an ident token, so we don't have a way to distinguish r#raw here, since ident is just a span and a symbol.
There was a problem hiding this comment.
Hmm 🤔 why is this being checked on the parsed expr and not as a lookahead after parsing the borrow modifiers? I guess kinda related to the recovery comment above, but would trivially allow checking for non-raw ident
There was a problem hiding this comment.
Well we do want to avoid putting the expectation down for something like &raw.field
|
r? oli-obk |
|
@bors r+ |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#139127 (Fix up partial res of segment in primitive resolution hack) - rust-lang#139392 (Detect and provide suggestion for `&raw EXPR`) - rust-lang#139767 (Visit place in `BackwardIncompatibleDropHint` statement) - rust-lang#139777 (Remove `define_debug_via_print` for `ExistentialProjection`, use regular structural debug impl) - rust-lang#139796 (ptr docs: add missing backtics around 'usize') - rust-lang#139801 (Add myself to mailmap) - rust-lang#139804 (use `realpath` in `bootstrap.py` when creating build-dir) - rust-lang#139807 (Improve wording of post-merge report) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#139392 - compiler-errors:raw-expr, r=oli-obk Detect and provide suggestion for `&raw EXPR` When emitting an error in the parser, and we detect that the previous token was `raw` and we *could* have consumed `const`/`mut`, suggest that this may have been a mistyped raw ref expr. To do this, we add `const`/`mut` to the expected token set when parsing `&raw` as an expression (which does not affect the "good path" of parsing, for the record). This is kind of a rudimentary error improvement, since it doesn't actually attempt to recover anything, leading to some other knock-on errors b/c we still treat `&raw` as the expression that was parsed... but at least we add the suggestion! I don't think the parser grammar means we can faithfully recover `&raw EXPR` early, i.e. during `parse_expr_borrow`. Fixes rust-lang#133231
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#139127 (Fix up partial res of segment in primitive resolution hack) - rust-lang#139392 (Detect and provide suggestion for `&raw EXPR`) - rust-lang#139767 (Visit place in `BackwardIncompatibleDropHint` statement) - rust-lang#139777 (Remove `define_debug_via_print` for `ExistentialProjection`, use regular structural debug impl) - rust-lang#139796 (ptr docs: add missing backtics around 'usize') - rust-lang#139801 (Add myself to mailmap) - rust-lang#139804 (use `realpath` in `bootstrap.py` when creating build-dir) - rust-lang#139807 (Improve wording of post-merge report) r? `@ghost` `@rustbot` modify labels: rollup
…=Kivooeo Don't recover `&raw EXPR` as a missing comma The parser already has a targeted suggestion for `&raw EXPR`, added in rust-lang#139392, telling the user that `&raw` must be followed by `const` or `mut`. In comma-separated expression lists, the generic missing-comma recovery would then treat the expression after `raw` as the next list element. That produced follow-up diagnostics like: - `help: missing ','` - `cannot find value raw in this scope` - an arity error for calls such as `takes_raw_ptr(&raw x)` This PR avoids that comma recovery when we are already in the `&raw` missing-`const`/`mut` diagnostic path. For function calls, it preserves the call expression with a single error argument, so useful later diagnostics such as `cannot find function f` are still emitted. Fixes rust-lang#157015.
…=Kivooeo Don't recover `&raw EXPR` as a missing comma The parser already has a targeted suggestion for `&raw EXPR`, added in rust-lang#139392, telling the user that `&raw` must be followed by `const` or `mut`. In comma-separated expression lists, the generic missing-comma recovery would then treat the expression after `raw` as the next list element. That produced follow-up diagnostics like: - `help: missing ','` - `cannot find value raw in this scope` - an arity error for calls such as `takes_raw_ptr(&raw x)` This PR avoids that comma recovery when we are already in the `&raw` missing-`const`/`mut` diagnostic path. For function calls, it preserves the call expression with a single error argument, so useful later diagnostics such as `cannot find function f` are still emitted. Fixes rust-lang#157015.
Don't recover `&raw EXPR` as a missing comma The parser already has a targeted suggestion for `&raw EXPR`, added in rust-lang/rust#139392, telling the user that `&raw` must be followed by `const` or `mut`. In comma-separated expression lists, the generic missing-comma recovery would then treat the expression after `raw` as the next list element. That produced follow-up diagnostics like: - `help: missing ','` - `cannot find value raw in this scope` - an arity error for calls such as `takes_raw_ptr(&raw x)` This PR avoids that comma recovery when we are already in the `&raw` missing-`const`/`mut` diagnostic path. For function calls, it preserves the call expression with a single error argument, so useful later diagnostics such as `cannot find function f` are still emitted. Fixes rust-lang/rust#157015.
When emitting an error in the parser, and we detect that the previous token was
rawand we could have consumedconst/mut, suggest that this may have been a mistyped raw ref expr. To do this, we addconst/mutto the expected token set when parsing&rawas an expression (which does not affect the "good path" of parsing, for the record).This is kind of a rudimentary error improvement, since it doesn't actually attempt to recover anything, leading to some other knock-on errors b/c we still treat
&rawas the expression that was parsed... but at least we add the suggestion! I don't think the parser grammar means we can faithfully recover&raw EXPRearly, i.e. duringparse_expr_borrow.Fixes #133231