tweak var/auto/mut recovery

This commit is contained in:
Mazdak Farrokhzad 2019-12-04 12:15:01 +01:00
parent 49826845a9
commit 621661f8a6
3 changed files with 39 additions and 31 deletions

View File

@ -42,16 +42,16 @@ impl<'a> Parser<'a> {
return self.parse_local_mk(lo, attrs.into()).map(Some)
}
if self.is_kw_followed_by_ident(kw::Mut) {
return self.recover_stmt_local(lo, attrs.into(), "missing `let`", "let mut");
return self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut");
}
if self.is_kw_followed_by_ident(kw::Auto) {
self.bump(); // `auto`
let msg = "to introduce a variable, write `let` instead of `auto`";
let msg = "write `let` instead of `auto` to introduce a new variable";
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}
if self.is_kw_followed_by_ident(sym::var) {
self.bump(); // `var`
let msg = "to introduce a variable, write `let` instead of `var`";
let msg = "write `let` instead of `var` to introduce a new variable";
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}
@ -208,14 +208,14 @@ impl<'a> Parser<'a> {
fn recover_stmt_local(
&mut self,
span: Span,
lo: Span,
attrs: AttrVec,
msg: &str,
sugg: &str,
) -> PResult<'a, Option<Stmt>> {
let stmt = self.parse_local_mk(span, attrs)?;
self.struct_span_err(stmt.span, "invalid variable declaration")
.span_suggestion_short(span, msg, sugg.to_string(), Applicability::MachineApplicable)
let stmt = self.parse_local_mk(lo, attrs)?;
self.struct_span_err(lo, "invalid variable declaration")
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
.emit();
Ok(Some(stmt))
}

View File

@ -1,20 +1,20 @@
fn main() {
auto n = 0;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `auto`
//~^ HELP write `let` instead of `auto` to introduce a new variable
auto m;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `auto`
//~^ HELP write `let` instead of `auto` to introduce a new variable
m = 0;
var n = 0;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `var`
//~^ HELP write `let` instead of `var` to introduce a new variable
var m;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `var`
//~^ HELP write `let` instead of `var` to introduce a new variable
m = 0;
mut n = 0;//~ ERROR invalid variable declaration
//~^ HELP missing `let`
//~^ HELP missing keyword
mut var;//~ ERROR invalid variable declaration
//~^ HELP missing `let`
//~^ HELP missing keyword
var = 0;
let _recovery_witness: () = 0; //~ ERROR mismatched types

View File

@ -2,49 +2,57 @@ error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:2:5
|
LL | auto n = 0;
| ----^^^^^^
| |
| help: to introduce a variable, write `let` instead of `auto`
| ^^^^
|
help: write `let` instead of `auto` to introduce a new variable
|
LL | let n = 0;
| ^^^
error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:4:5
|
LL | auto m;
| ----^^
| |
| help: to introduce a variable, write `let` instead of `auto`
| ^^^^
|
help: write `let` instead of `auto` to introduce a new variable
|
LL | let m;
| ^^^
error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:8:5
|
LL | var n = 0;
| ---^^^^^^
| |
| help: to introduce a variable, write `let` instead of `var`
| ^^^
|
help: write `let` instead of `var` to introduce a new variable
|
LL | let n = 0;
| ^^^
error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:10:5
|
LL | var m;
| ---^^
| |
| help: to introduce a variable, write `let` instead of `var`
| ^^^
|
help: write `let` instead of `var` to introduce a new variable
|
LL | let m;
| ^^^
error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:14:5
|
LL | mut n = 0;
| ---^^^^^^
| |
| help: missing `let`
| ^^^ help: missing keyword: `let mut`
error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:16:5
|
LL | mut var;
| ---^^^^
| |
| help: missing `let`
| ^^^ help: missing keyword: `let mut`
error[E0308]: mismatched types
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:20:33