Skip to content

Commit 5e94b5f

Browse files
committed
code refactor and fix wrong suggestion
1 parent a7fc32c commit 5e94b5f

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+29-23
Original file line numberDiff line numberDiff line change
@@ -399,29 +399,6 @@ 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-
match self.parse_ty() {
409-
Ok(_) => {
410-
if self.token == token::Eq {
411-
err.span_suggestion_verbose(
412-
prev_span,
413-
"you might have meant to introduce a new binding",
414-
"let ".to_string(),
415-
Applicability::MaybeIncorrect,
416-
);
417-
}
418-
}
419-
Err(err) => {
420-
err.cancel();
421-
}
422-
}
423-
self.restore_snapshot(snapshot);
424-
}
425402

426403
if let Some(recovered_ident) = recovered_ident && recover {
427404
err.emit();
@@ -1029,6 +1006,35 @@ impl<'a> Parser<'a> {
10291006
Err(e)
10301007
}
10311008

1009+
/// Suggest add the missing `let` before the identifier in stmt
1010+
/// `a: Ty = 1` -> `let a: Ty = 1`
1011+
pub(super) fn suggest_add_missing_let_for_stmt(
1012+
&mut self,
1013+
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
1014+
) {
1015+
if self.token == token::Colon {
1016+
let prev_span = self.prev_token.span.shrink_to_lo();
1017+
let snapshot = self.create_snapshot_for_diagnostic();
1018+
self.bump();
1019+
match self.parse_ty() {
1020+
Ok(_) => {
1021+
if self.token == token::Eq {
1022+
err.span_suggestion_verbose(
1023+
prev_span,
1024+
"you might have meant to introduce a new binding",
1025+
"let ".to_string(),
1026+
Applicability::MaybeIncorrect,
1027+
);
1028+
}
1029+
}
1030+
Err(e) => {
1031+
e.cancel();
1032+
}
1033+
}
1034+
self.restore_snapshot(snapshot);
1035+
}
1036+
}
1037+
10321038
/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
10331039
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
10341040
/// parenthesising the leftmost comparison.

compiler/rustc_parse/src/parser/stmt.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ impl<'a> Parser<'a> {
9999
ForceCollect::Yes => {
100100
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))?
101101
}
102-
ForceCollect::No => self.parse_stmt_path_start(lo, attrs)?,
102+
ForceCollect::No => match self.parse_stmt_path_start(lo, attrs) {
103+
Ok(stmt) => stmt,
104+
Err(mut err) => {
105+
self.suggest_add_missing_let_for_stmt(&mut err);
106+
return Err(err);
107+
}
108+
},
103109
}
104110
} else if let Some(item) = self.parse_item_common(
105111
attrs.clone(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct A {
2+
: u8 =, //~ ERROR expected identifier, found `:`
3+
}
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected identifier, found `:`
2+
--> $DIR/missing-let-in-binding-4.rs:2:5
3+
|
4+
LL | struct A {
5+
| - while parsing this struct
6+
LL | : u8 =,
7+
| ^ expected identifier
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)