1
Fork 0

Rollup merge of #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 #133231
This commit is contained in:
Matthias Krüger 2025-04-14 18:15:31 +02:00 committed by GitHub
commit bf49dfc943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 189 additions and 1 deletions

View file

@ -518,7 +518,11 @@ impl<'a> Parser<'a> {
let prev = self.prev_token.span;
let sp = self.token.span;
let mut e = self.dcx().struct_span_err(sp, msg);
let do_not_suggest_help = self.token.is_keyword(kw::In) || self.token == token::Colon;
self.label_expected_raw_ref(&mut e);
let do_not_suggest_help = self.token.is_keyword(kw::In)
|| self.token == token::Colon
|| self.prev_token.is_keyword(kw::Raw);
// Check to see if the user has written something like
//