-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Detect and provide suggestion for &raw EXPR
#139392
base: master
Are you sure you want to change the base?
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.
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