Skip to content

Commit 4ff7aff

Browse files
committed
add suggestion to add mut or const after &raw
1 parent 2006117 commit 4ff7aff

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ parse_suffixed_literal_in_attribute = suffixed literals are not allowed in attri
764764
765765
parse_sugg_add_let_for_stmt = you might have meant to introduce a new binding
766766
767+
parse_sugg_add_mut_or_const_in_raw_ref = you might have meant to use a raw reference
768+
767769
parse_sugg_add_semi = add `;` here
768770
parse_sugg_change_inner_attr_to_outer = to annotate the {$item}, change the attribute from inner to outer style
769771

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,22 @@ impl<'a> Parser<'a> {
13031303
Err(e)
13041304
}
13051305

1306+
/// When writing `&raw` that is missing the following `mut` or `const`, we will
1307+
/// encounter a parse error when encountering the next expression.
1308+
pub(super) fn suggest_add_mut_or_const_in_raw_ref(&mut self, e: &mut Diag<'_>, expr: &P<Expr>) {
1309+
if let ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, r) = &expr.kind
1310+
&& let ast::ExprKind::Path(_, p) = &r.kind
1311+
&& *p == Symbol::intern("raw")
1312+
{
1313+
e.span_suggestions(
1314+
expr.span.shrink_to_hi(),
1315+
fluent::parse_sugg_add_mut_or_const_in_raw_ref,
1316+
[" mut".to_string(), " const".to_string()],
1317+
Applicability::HasPlaceholders,
1318+
);
1319+
}
1320+
}
1321+
13061322
/// Suggest add the missing `let` before the identifier in stmt
13071323
/// `a: Ty = 1` -> `let a: Ty = 1`
13081324
pub(super) fn suggest_add_missing_let_for_stmt(&mut self, err: &mut Diag<'a>) {

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,10 @@ impl<'a> Parser<'a> {
15811581
ExprKind::Array(exprs)
15821582
} else {
15831583
// Vector with one element
1584-
self.expect(close)?;
1584+
self.expect(close).map_err(|mut e| {
1585+
self.suggest_add_mut_or_const_in_raw_ref(&mut e, &first_expr);
1586+
e
1587+
})?;
15851588
ExprKind::Array(thin_vec![first_expr])
15861589
}
15871590
};

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ impl<'a> Parser<'a> {
897897
res?
898898
} else {
899899
res.unwrap_or_else(|mut e| {
900+
self.suggest_add_mut_or_const_in_raw_ref(&mut e, expr);
900901
self.recover_missing_dot(&mut e);
901902
let guar = e.emit();
902903
self.recover_stmt();
@@ -920,6 +921,7 @@ impl<'a> Parser<'a> {
920921
LocalKind::Init(expr) | LocalKind::InitElse(expr, _) => {
921922
self.check_mistyped_turbofish_with_multiple_type_params(e, expr).map_err(
922923
|mut e| {
924+
self.suggest_add_mut_or_const_in_raw_ref(&mut e, expr);
923925
self.recover_missing_dot(&mut e);
924926
e
925927
},

0 commit comments

Comments
 (0)