-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Detect and provide suggestion for &raw EXPR
#139392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? @davidtwco rustbot has assigned @davidtwco. Use |
@@ -829,6 +829,18 @@ impl<'a> Parser<'a> { | |||
if let Some(lt) = lifetime { | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we do want to avoid putting the expectation down for something like &raw.field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yea
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
When emitting an error in the parser, and we detect that the previous token was
raw
and we could have consumedconst
/mut
, suggest that this may have been a mistyped raw ref expr. To do this, we addconst
/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. duringparse_expr_borrow
.Fixes #133231