Skip to content

Commit 0bb43c6

Browse files
committed
Suggest let for possible binding with ty
1 parent ad6b20b commit 0bb43c6

9 files changed

+61
-12
lines changed

compiler/rustc_macros/src/diagnostics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn lint_diagnostic_derive(s: Structure<'_>) -> TokenStream {
140140
/// ```fluent
141141
/// parser_expected_identifier = expected identifier
142142
///
143-
/// parser_expected_identifier-found = expected identifier, found {$found}
143+
/// parser_expected_identifier_found = expected identifier, found {$found}
144144
///
145145
/// parser_raw_identifier = escape `{$ident}` to use it as an identifier
146146
/// ```

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,23 @@ impl<'a> Parser<'a> {
399399
}
400400
}
401401
}
402+
// we suggest add the missing `let` before the identifier
403+
// `a: Ty = 1` -> `let a: Ty = 1`
404+
if self.token == token::Colon {
405+
let prev_span = self.prev_token.span.shrink_to_lo();
406+
let snapshot = self.create_snapshot_for_diagnostic();
407+
self.bump();
408+
let res = self.parse_ty();
409+
if res.is_ok() && self.token == token::Eq {
410+
err.span_suggestion_verbose(
411+
prev_span,
412+
"you might have meant to introduce a new binding",
413+
"let ".to_string(),
414+
Applicability::MaybeIncorrect,
415+
);
416+
}
417+
self.restore_snapshot(snapshot);
418+
}
402419

403420
if let Some(recovered_ident) = recovered_ident && recover {
404421
err.emit();

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ impl<'a> Parser<'a> {
555555
if self.token == token::Colon {
556556
// if next token is following a colon, it's likely a path
557557
// and we can suggest a path separator
558-
let ident_span = self.prev_token.span;
559558
self.bump();
560559
if self.token.span.lo() == self.prev_token.span.hi() {
561560
err.span_suggestion_verbose(
@@ -565,14 +564,6 @@ impl<'a> Parser<'a> {
565564
Applicability::MaybeIncorrect,
566565
);
567566
}
568-
if self.look_ahead(1, |token| token == &token::Eq) {
569-
err.span_suggestion_verbose(
570-
ident_span.shrink_to_lo(),
571-
"you might have meant to introduce a new binding",
572-
"let ",
573-
Applicability::MaybeIncorrect,
574-
);
575-
}
576567
if self.sess.unstable_features.is_nightly_build() {
577568
// FIXME(Nilstrieb): Remove this again after a few months.
578569
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-rustfix
2+
3+
fn fun(x: i32) -> i32 { x }
4+
5+
fn main() {
6+
let _closure_annotated = |value: i32| -> i32 {
7+
let temp: i32 = fun(5i32);
8+
//~^ ERROR expected identifier, found `:`
9+
temp + value + 1
10+
};
11+
}

tests/ui/suggestions/type-ascription-instead-of-let.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// run-rustfix
2+
13
fn fun(x: i32) -> i32 { x }
24

35
fn main() {
4-
let closure_annotated = |value: i32| -> i32 {
6+
let _closure_annotated = |value: i32| -> i32 {
57
temp: i32 = fun(5i32);
68
//~^ ERROR expected identifier, found `:`
79
temp + value + 1
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
error: expected identifier, found `:`
2-
--> $DIR/type-ascription-instead-of-let.rs:5:13
2+
--> $DIR/type-ascription-instead-of-let.rs:7:13
33
|
44
LL | temp: i32 = fun(5i32);
55
| ^ expected identifier
6+
|
7+
help: you might have meant to introduce a new binding
8+
|
9+
LL | let temp: i32 = fun(5i32);
10+
| +++
611

712
error: aborting due to previous error
813

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let _v: Vec<i32> = vec![1, 2, 3]; //~ ERROR expected identifier, found `:`
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
_v: Vec<i32> = vec![1, 2, 3]; //~ ERROR expected identifier, found `:`
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected identifier, found `:`
2+
--> $DIR/missing-let-in-binding-2.rs:4:7
3+
|
4+
LL | _v: Vec<i32> = vec![1, 2, 3];
5+
| ^ expected identifier
6+
|
7+
help: you might have meant to introduce a new binding
8+
|
9+
LL | let _v: Vec<i32> = vec![1, 2, 3];
10+
| +++
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)