Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

compiler-errors
Copy link
Member

@compiler-errors compiler-errors commented Apr 4, 2025

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 #133231

@rustbot
Copy link
Collaborator

rustbot commented Apr 4, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 4, 2025
@@ -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.
Copy link
Member Author

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))
Copy link
Member

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

Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terse parse error on &raw expr
4 participants