|
1 | 1 | use super::attr::DEFAULT_INNER_ATTR_FORBIDDEN; |
2 | | -use super::diagnostics::{AttemptLocalParseRecovery, Error}; |
| 2 | +use super::diagnostics::{ |
| 3 | + AttemptLocalParseRecovery, Error, InvalidVariableDeclaration, InvalidVariableDeclarationSub, |
| 4 | +}; |
3 | 5 | use super::expr::LhsExpr; |
4 | 6 | use super::pat::RecoverComma; |
5 | 7 | use super::path::PathStyle; |
@@ -58,28 +60,22 @@ impl<'a> Parser<'a> { |
58 | 60 | if self.token.is_keyword(kw::Mut) && self.is_keyword_ahead(1, &[kw::Let]) { |
59 | 61 | self.bump(); |
60 | 62 | let mut_let_span = lo.to(self.token.span); |
61 | | - self.struct_span_err(mut_let_span, "invalid variable declaration") |
62 | | - .span_suggestion( |
63 | | - mut_let_span, |
64 | | - "switch the order of `mut` and `let`", |
65 | | - "let mut", |
66 | | - Applicability::MaybeIncorrect, |
67 | | - ) |
68 | | - .emit(); |
| 63 | + self.sess.emit_err(InvalidVariableDeclaration { |
| 64 | + span: mut_let_span, |
| 65 | + sub: InvalidVariableDeclarationSub::SwitchMutLetOrder(mut_let_span), |
| 66 | + }); |
69 | 67 | } |
70 | 68 |
|
71 | 69 | Ok(Some(if self.token.is_keyword(kw::Let) { |
72 | 70 | self.parse_local_mk(lo, attrs, capture_semi, force_collect)? |
73 | 71 | } else if self.is_kw_followed_by_ident(kw::Mut) { |
74 | | - self.recover_stmt_local(lo, attrs, "missing keyword", "let mut")? |
| 72 | + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::MissingLet)? |
75 | 73 | } else if self.is_kw_followed_by_ident(kw::Auto) { |
76 | 74 | self.bump(); // `auto` |
77 | | - let msg = "write `let` instead of `auto` to introduce a new variable"; |
78 | | - self.recover_stmt_local(lo, attrs, msg, "let")? |
| 75 | + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotAuto)? |
79 | 76 | } else if self.is_kw_followed_by_ident(sym::var) { |
80 | 77 | self.bump(); // `var` |
81 | | - let msg = "write `let` instead of `var` to introduce a new variable"; |
82 | | - self.recover_stmt_local(lo, attrs, msg, "let")? |
| 78 | + self.recover_stmt_local(lo, attrs, InvalidVariableDeclarationSub::UseLetNotVar)? |
83 | 79 | } else if self.check_path() && !self.token.is_qpath_start() && !self.is_path_start_item() { |
84 | 80 | // We have avoided contextual keywords like `union`, items with `crate` visibility, |
85 | 81 | // or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something |
@@ -217,13 +213,10 @@ impl<'a> Parser<'a> { |
217 | 213 | &mut self, |
218 | 214 | lo: Span, |
219 | 215 | attrs: AttrWrapper, |
220 | | - msg: &str, |
221 | | - sugg: &str, |
| 216 | + subdiagnostic: fn(Span) -> InvalidVariableDeclarationSub, |
222 | 217 | ) -> PResult<'a, Stmt> { |
223 | 218 | let stmt = self.recover_local_after_let(lo, attrs)?; |
224 | | - self.struct_span_err(lo, "invalid variable declaration") |
225 | | - .span_suggestion(lo, msg, sugg, Applicability::MachineApplicable) |
226 | | - .emit(); |
| 219 | + self.sess.emit_err(InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) }); |
227 | 220 | Ok(stmt) |
228 | 221 | } |
229 | 222 |
|
|
0 commit comments