Skip to content

Commit

Permalink
tweak var/auto/mut recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Dec 20, 2019
1 parent 4982684 commit 621661f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
14 changes: 7 additions & 7 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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))
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/parser/issue-65257-invalid-var-decl-recovery.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 26 additions & 18 deletions src/test/ui/parser/issue-65257-invalid-var-decl-recovery.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 621661f

Please sign in to comment.