Skip to content

Commit

Permalink
code refactor and fix wrong suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed May 8, 2023
1 parent a7fc32c commit 5e94b5f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
52 changes: 29 additions & 23 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,29 +399,6 @@ impl<'a> Parser<'a> {
}
}
}
// we suggest add the missing `let` before the identifier
// `a: Ty = 1` -> `let a: Ty = 1`
if self.token == token::Colon {
let prev_span = self.prev_token.span.shrink_to_lo();
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Ok(_) => {
if self.token == token::Eq {
err.span_suggestion_verbose(
prev_span,
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
Err(err) => {
err.cancel();
}
}
self.restore_snapshot(snapshot);
}

if let Some(recovered_ident) = recovered_ident && recover {
err.emit();
Expand Down Expand Up @@ -1029,6 +1006,35 @@ impl<'a> Parser<'a> {
Err(e)
}

/// Suggest add the missing `let` before the identifier in stmt
/// `a: Ty = 1` -> `let a: Ty = 1`
pub(super) fn suggest_add_missing_let_for_stmt(
&mut self,
err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
) {
if self.token == token::Colon {
let prev_span = self.prev_token.span.shrink_to_lo();
let snapshot = self.create_snapshot_for_diagnostic();
self.bump();
match self.parse_ty() {
Ok(_) => {
if self.token == token::Eq {
err.span_suggestion_verbose(
prev_span,
"you might have meant to introduce a new binding",
"let ".to_string(),
Applicability::MaybeIncorrect,
);
}
}
Err(e) => {
e.cancel();
}
}
self.restore_snapshot(snapshot);
}
}

/// Check to see if a pair of chained operators looks like an attempt at chained comparison,
/// e.g. `1 < x <= 3`. If so, suggest either splitting the comparison into two, or
/// parenthesising the leftmost comparison.
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ impl<'a> Parser<'a> {
ForceCollect::Yes => {
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs))?
}
ForceCollect::No => self.parse_stmt_path_start(lo, attrs)?,
ForceCollect::No => match self.parse_stmt_path_start(lo, attrs) {
Ok(stmt) => stmt,
Err(mut err) => {
self.suggest_add_missing_let_for_stmt(&mut err);
return Err(err);
}
},
}
} else if let Some(item) = self.parse_item_common(
attrs.clone(),
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/type/missing-let-in-binding-4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct A {
: u8 =, //~ ERROR expected identifier, found `:`
}

fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/type/missing-let-in-binding-4.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected identifier, found `:`
--> $DIR/missing-let-in-binding-4.rs:2:5
|
LL | struct A {
| - while parsing this struct
LL | : u8 =,
| ^ expected identifier

error: aborting due to previous error

0 comments on commit 5e94b5f

Please sign in to comment.