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:
commit
bf49dfc943
6 changed files with 189 additions and 1 deletions
|
@ -609,6 +609,8 @@ impl<'a> Parser<'a> {
|
|||
// FIXME: translation requires list formatting (for `expect`)
|
||||
let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
|
||||
|
||||
self.label_expected_raw_ref(&mut err);
|
||||
|
||||
// Look for usages of '=>' where '>=' was probably intended
|
||||
if self.token == token::FatArrow
|
||||
&& expected.iter().any(|tok| matches!(tok, TokenType::Operator | TokenType::Le))
|
||||
|
@ -750,6 +752,25 @@ impl<'a> Parser<'a> {
|
|||
Err(err)
|
||||
}
|
||||
|
||||
/// Adds a label when `&raw EXPR` was written instead of `&raw const EXPR`/`&raw mut EXPR`.
|
||||
///
|
||||
/// Given that not all parser diagnostics flow through `expected_one_of_not_found`, this
|
||||
/// label may need added to other diagnostics emission paths as needed.
|
||||
pub(super) fn label_expected_raw_ref(&mut self, err: &mut Diag<'_>) {
|
||||
if self.prev_token.is_keyword(kw::Raw)
|
||||
&& self.expected_token_types.contains(TokenType::KwMut)
|
||||
&& self.expected_token_types.contains(TokenType::KwConst)
|
||||
&& self.token.can_begin_expr()
|
||||
{
|
||||
err.span_suggestions(
|
||||
self.prev_token.span.shrink_to_hi(),
|
||||
"`&raw` must be followed by `const` or `mut` to be a raw reference expression",
|
||||
[" const".to_string(), " mut".to_string()],
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the current token or the previous token are misspelled keywords
|
||||
/// and adds a helpful suggestion.
|
||||
fn check_for_misspelled_kw(&self, err: &mut Diag<'_>, expected: &[TokenType]) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue