about summary refs log tree commit diff
path: root/src/librustc_parse/parser
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-08-12 08:40:36 +0000
committerbors <bors@rust-lang.org>2020-08-12 08:40:36 +0000
commit5989bf48724031b72326a5b731a15fca101339e2 (patch)
tree01c1ef34b5adf27e4d5188d30e44317570a61b02 /src/librustc_parse/parser
parent4745cbe83e0b3299bfe7f7f305b975c3c09f92db (diff)
parent308b5585f37ccc71d58f9afc43c573ce02b339d0 (diff)
Auto merge of #75321 - estebank:js-goes-gaga, r=davidtwco
Detect JS-style `===` and `!==` and recover

Fix #75312.
Diffstat (limited to 'src/librustc_parse/parser')
-rw-r--r--src/librustc_parse/parser/expr.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs
index 55a134ae091..9b99eb2e0bd 100644
--- a/src/librustc_parse/parser/expr.rs
+++ b/src/librustc_parse/parser/expr.rs
@@ -195,6 +195,29 @@ impl<'a> Parser<'a> {
                     return Ok(expr);
                 }
             }
+
+            if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual)
+                && self.token.kind == token::Eq
+                && self.prev_token.span.hi() == self.token.span.lo()
+            {
+                // Look for JS' `===` and `!==` and recover 😇
+                let sp = op.span.to(self.token.span);
+                let sugg = match op.node {
+                    AssocOp::Equal => "==",
+                    AssocOp::NotEqual => "!=",
+                    _ => unreachable!(),
+                };
+                self.struct_span_err(sp, &format!("invalid comparison operator `{}=`", sugg))
+                    .span_suggestion_short(
+                        sp,
+                        &format!("`{s}=` is not a valid comparison operator, use `{s}`", s = sugg),
+                        sugg.to_string(),
+                        Applicability::MachineApplicable,
+                    )
+                    .emit();
+                self.bump();
+            }
+
             let op = op.node;
             // Special cases:
             if op == AssocOp::As {