Skip to content

Commit ddb225f

Browse files
committed
fixes #101477: Recover from typo where == is used in place of =
1 parent e7c7aa7 commit ddb225f

File tree

8 files changed

+73
-0
lines changed

8 files changed

+73
-0
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,6 @@ parser_left_arrow_operator = unexpected token: `<-`
153153
154154
parser_remove_let = expected pattern, found `let`
155155
.suggestion = remove the unnecessary `let` keyword
156+
157+
parser_use_eq_instead = unexpected `==`
158+
.suggestion = try using `=` instead

compiler/rustc_parse/src/parser/diagnostics.rs

+16
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ pub(crate) struct RemoveLet {
713713
pub span: Span,
714714
}
715715

716+
#[derive(SessionDiagnostic)]
717+
#[diag(parser::use_eq_instead)]
718+
pub(crate) struct UseEqInstead {
719+
#[primary_span]
720+
#[suggestion_short(applicability = "machine-applicable", code = "=")]
721+
pub span: Span,
722+
}
723+
716724
// SnapshotParser is used to create a snapshot of the parser
717725
// without causing duplicate errors being emitted when the `Parser`
718726
// is dropped.
@@ -957,6 +965,14 @@ impl<'a> Parser<'a> {
957965
}
958966
}
959967

968+
if self.token.kind == TokenKind::EqEq
969+
&& self.prev_token.is_ident()
970+
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
971+
{
972+
// Likely typo: `=` → `==` in let expr or enum item
973+
return Err(self.sess.create_err(UseEqInstead { span: self.token.span }));
974+
}
975+
960976
let expect = tokens_to_string(&expected);
961977
let actual = super::token_descr(&self.token);
962978
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
3+
#[allow(dead_code)]
4+
enum Demo {
5+
A = 1,
6+
B = 2 //~ ERROR unexpected `==`
7+
//~^ expected item, found `==`
8+
}
9+
10+
fn main() {}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
3+
#[allow(dead_code)]
4+
enum Demo {
5+
A = 1,
6+
B == 2 //~ ERROR unexpected `==`
7+
//~^ expected item, found `==`
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unexpected `==`
2+
--> $DIR/issue-101477-enum.rs:6:7
3+
|
4+
LL | B == 2
5+
| ^^ help: try using `=` instead
6+
7+
error: expected item, found `==`
8+
--> $DIR/issue-101477-enum.rs:6:7
9+
|
10+
LL | B == 2
11+
| ^^ expected item
12+
13+
error: aborting due to 2 previous errors
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let x = 2; //~ ERROR unexpected `==`
5+
println!("x: {}", x)
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
let x == 2; //~ ERROR unexpected `==`
5+
println!("x: {}", x)
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unexpected `==`
2+
--> $DIR/issue-101477-let.rs:4:11
3+
|
4+
LL | let x == 2;
5+
| ^^ help: try using `=` instead
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)