Skip to content

Commit 621661f

Browse files
committed
tweak var/auto/mut recovery
1 parent 4982684 commit 621661f

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/librustc_parse/parser/stmt.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ impl<'a> Parser<'a> {
4242
return self.parse_local_mk(lo, attrs.into()).map(Some)
4343
}
4444
if self.is_kw_followed_by_ident(kw::Mut) {
45-
return self.recover_stmt_local(lo, attrs.into(), "missing `let`", "let mut");
45+
return self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut");
4646
}
4747
if self.is_kw_followed_by_ident(kw::Auto) {
4848
self.bump(); // `auto`
49-
let msg = "to introduce a variable, write `let` instead of `auto`";
49+
let msg = "write `let` instead of `auto` to introduce a new variable";
5050
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
5151
}
5252
if self.is_kw_followed_by_ident(sym::var) {
5353
self.bump(); // `var`
54-
let msg = "to introduce a variable, write `let` instead of `var`";
54+
let msg = "write `let` instead of `var` to introduce a new variable";
5555
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
5656
}
5757

@@ -208,14 +208,14 @@ impl<'a> Parser<'a> {
208208

209209
fn recover_stmt_local(
210210
&mut self,
211-
span: Span,
211+
lo: Span,
212212
attrs: AttrVec,
213213
msg: &str,
214214
sugg: &str,
215215
) -> PResult<'a, Option<Stmt>> {
216-
let stmt = self.parse_local_mk(span, attrs)?;
217-
self.struct_span_err(stmt.span, "invalid variable declaration")
218-
.span_suggestion_short(span, msg, sugg.to_string(), Applicability::MachineApplicable)
216+
let stmt = self.parse_local_mk(lo, attrs)?;
217+
self.struct_span_err(lo, "invalid variable declaration")
218+
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
219219
.emit();
220220
Ok(Some(stmt))
221221
}

src/test/ui/parser/issue-65257-invalid-var-decl-recovery.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
fn main() {
22
auto n = 0;//~ ERROR invalid variable declaration
3-
//~^ HELP to introduce a variable, write `let` instead of `auto`
3+
//~^ HELP write `let` instead of `auto` to introduce a new variable
44
auto m;//~ ERROR invalid variable declaration
5-
//~^ HELP to introduce a variable, write `let` instead of `auto`
5+
//~^ HELP write `let` instead of `auto` to introduce a new variable
66
m = 0;
77

88
var n = 0;//~ ERROR invalid variable declaration
9-
//~^ HELP to introduce a variable, write `let` instead of `var`
9+
//~^ HELP write `let` instead of `var` to introduce a new variable
1010
var m;//~ ERROR invalid variable declaration
11-
//~^ HELP to introduce a variable, write `let` instead of `var`
11+
//~^ HELP write `let` instead of `var` to introduce a new variable
1212
m = 0;
1313

1414
mut n = 0;//~ ERROR invalid variable declaration
15-
//~^ HELP missing `let`
15+
//~^ HELP missing keyword
1616
mut var;//~ ERROR invalid variable declaration
17-
//~^ HELP missing `let`
17+
//~^ HELP missing keyword
1818
var = 0;
1919

2020
let _recovery_witness: () = 0; //~ ERROR mismatched types

src/test/ui/parser/issue-65257-invalid-var-decl-recovery.stderr

+26-18
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,57 @@ error: invalid variable declaration
22
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:2:5
33
|
44
LL | auto n = 0;
5-
| ----^^^^^^
6-
| |
7-
| help: to introduce a variable, write `let` instead of `auto`
5+
| ^^^^
6+
|
7+
help: write `let` instead of `auto` to introduce a new variable
8+
|
9+
LL | let n = 0;
10+
| ^^^
811

912
error: invalid variable declaration
1013
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:4:5
1114
|
1215
LL | auto m;
13-
| ----^^
14-
| |
15-
| help: to introduce a variable, write `let` instead of `auto`
16+
| ^^^^
17+
|
18+
help: write `let` instead of `auto` to introduce a new variable
19+
|
20+
LL | let m;
21+
| ^^^
1622

1723
error: invalid variable declaration
1824
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:8:5
1925
|
2026
LL | var n = 0;
21-
| ---^^^^^^
22-
| |
23-
| help: to introduce a variable, write `let` instead of `var`
27+
| ^^^
28+
|
29+
help: write `let` instead of `var` to introduce a new variable
30+
|
31+
LL | let n = 0;
32+
| ^^^
2433

2534
error: invalid variable declaration
2635
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:10:5
2736
|
2837
LL | var m;
29-
| ---^^
30-
| |
31-
| help: to introduce a variable, write `let` instead of `var`
38+
| ^^^
39+
|
40+
help: write `let` instead of `var` to introduce a new variable
41+
|
42+
LL | let m;
43+
| ^^^
3244

3345
error: invalid variable declaration
3446
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:14:5
3547
|
3648
LL | mut n = 0;
37-
| ---^^^^^^
38-
| |
39-
| help: missing `let`
49+
| ^^^ help: missing keyword: `let mut`
4050

4151
error: invalid variable declaration
4252
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:16:5
4353
|
4454
LL | mut var;
45-
| ---^^^^
46-
| |
47-
| help: missing `let`
55+
| ^^^ help: missing keyword: `let mut`
4856

4957
error[E0308]: mismatched types
5058
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:20:33

0 commit comments

Comments
 (0)