Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from typo where == is used in place of = #101515

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/parser.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,6 @@ parser_left_arrow_operator = unexpected token: `<-`

parser_remove_let = expected pattern, found `let`
.suggestion = remove the unnecessary `let` keyword

parser_use_eq_instead = unexpected `==`
.suggestion = try using `=` instead
16 changes: 16 additions & 0 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ pub(crate) struct RemoveLet {
pub span: Span,
}

#[derive(SessionDiagnostic)]
#[diag(parser::use_eq_instead)]
pub(crate) struct UseEqInstead {
#[primary_span]
#[suggestion_short(applicability = "machine-applicable", code = "=")]
pub span: Span,
}

// SnapshotParser is used to create a snapshot of the parser
// without causing duplicate errors being emitted when the `Parser`
// is dropped.
Expand Down Expand Up @@ -957,6 +965,14 @@ impl<'a> Parser<'a> {
}
}

if self.token.kind == TokenKind::EqEq
&& self.prev_token.is_ident()
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
{
// Likely typo: `=` → `==` in let expr or enum item
return Err(self.sess.create_err(UseEqInstead { span: self.token.span }));
}

let expect = tokens_to_string(&expected);
let actual = super::token_descr(&self.token);
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/parser/issue-101477-enum.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix

#[allow(dead_code)]
enum Demo {
A = 1,
B = 2 //~ ERROR unexpected `==`
//~^ expected item, found `==`
}

fn main() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/issue-101477-enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix

#[allow(dead_code)]
enum Demo {
A = 1,
B == 2 //~ ERROR unexpected `==`
//~^ expected item, found `==`
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/parser/issue-101477-enum.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: unexpected `==`
--> $DIR/issue-101477-enum.rs:6:7
|
LL | B == 2
| ^^ help: try using `=` instead

error: expected item, found `==`
--> $DIR/issue-101477-enum.rs:6:7
|
LL | B == 2
| ^^ expected item

error: aborting due to 2 previous errors

6 changes: 6 additions & 0 deletions src/test/ui/parser/issue-101477-let.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix

fn main() {
let x = 2; //~ ERROR unexpected `==`
println!("x: {}", x)
}
6 changes: 6 additions & 0 deletions src/test/ui/parser/issue-101477-let.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// run-rustfix

fn main() {
let x == 2; //~ ERROR unexpected `==`
println!("x: {}", x)
}
8 changes: 8 additions & 0 deletions src/test/ui/parser/issue-101477-let.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `==`
--> $DIR/issue-101477-let.rs:4:11
|
LL | let x == 2;
| ^^ help: try using `=` instead

error: aborting due to previous error