]> git.lizzy.rs Git - rust.git/commitdiff
Recover on invalid operators <> and <=>
authorr00ster91 <r00ster91@protonmail.com>
Mon, 6 Dec 2021 17:55:58 +0000 (18:55 +0100)
committerr00ster91 <r00ster91@protonmail.com>
Tue, 14 Dec 2021 17:05:02 +0000 (18:05 +0100)
compiler/rustc_parse/src/parser/expr.rs
src/test/ui/operator-recovery/less-than-greater-than.rs [new file with mode: 0644]
src/test/ui/operator-recovery/less-than-greater-than.stderr [new file with mode: 0644]
src/test/ui/operator-recovery/spaceship.rs [new file with mode: 0644]
src/test/ui/operator-recovery/spaceship.stderr [new file with mode: 0644]

index 1dbd7bad0f0236d416885e9b74123d52b9942916..1c31c84b8bfec6161c3575328609bd8f318c1406 100644 (file)
@@ -213,11 +213,11 @@ pub(super) fn parse_assoc_expr_with(
                 }
             }
 
+            // Look for JS' `===` and `!==` and recover
             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 => "==",
@@ -235,6 +235,38 @@ pub(super) fn parse_assoc_expr_with(
                 self.bump();
             }
 
+            // Look for PHP's `<>` and recover
+            if op.node == AssocOp::Less
+                && self.token.kind == token::Gt
+                && self.prev_token.span.hi() == self.token.span.lo()
+            {
+                let sp = op.span.to(self.token.span);
+                self.struct_span_err(sp, "invalid comparison operator `<>`")
+                    .span_suggestion_short(
+                        sp,
+                        "`<>` is not a valid comparison operator, use `!=`",
+                        "!=".to_string(),
+                        Applicability::MachineApplicable,
+                    )
+                    .emit();
+                self.bump();
+            }
+
+            // Look for C++'s `<=>` and recover
+            if op.node == AssocOp::LessEqual
+                && self.token.kind == token::Gt
+                && self.prev_token.span.hi() == self.token.span.lo()
+            {
+                let sp = op.span.to(self.token.span);
+                self.struct_span_err(sp, "invalid comparison operator `<=>`")
+                    .span_label(
+                        sp,
+                        "`<=>` is not a valid comparison operator, use `std::cmp::Ordering`",
+                    )
+                    .emit();
+                self.bump();
+            }
+
             let op = op.node;
             // Special cases:
             if op == AssocOp::As {
diff --git a/src/test/ui/operator-recovery/less-than-greater-than.rs b/src/test/ui/operator-recovery/less-than-greater-than.rs
new file mode 100644 (file)
index 0000000..2beed52
--- /dev/null
@@ -0,0 +1,4 @@
+fn main() {
+    println!("{}", 1 <> 2);
+    //~^ERROR invalid comparison operator `<>`
+}
diff --git a/src/test/ui/operator-recovery/less-than-greater-than.stderr b/src/test/ui/operator-recovery/less-than-greater-than.stderr
new file mode 100644 (file)
index 0000000..80c9215
--- /dev/null
@@ -0,0 +1,8 @@
+error: invalid comparison operator `<>`
+  --> $DIR/less-than-greater-than.rs:2:22
+   |
+LL |     println!("{}", 1 <> 2);
+   |                      ^^ help: `<>` is not a valid comparison operator, use `!=`
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/operator-recovery/spaceship.rs b/src/test/ui/operator-recovery/spaceship.rs
new file mode 100644 (file)
index 0000000..a65f938
--- /dev/null
@@ -0,0 +1,4 @@
+fn main() {
+    println!("{}", 1 <=> 2);
+    //~^ERROR invalid comparison operator `<=>`
+}
diff --git a/src/test/ui/operator-recovery/spaceship.stderr b/src/test/ui/operator-recovery/spaceship.stderr
new file mode 100644 (file)
index 0000000..ed6bd74
--- /dev/null
@@ -0,0 +1,8 @@
+error: invalid comparison operator `<=>`
+  --> $DIR/spaceship.rs:2:22
+   |
+LL |     println!("{}", 1 <=> 2);
+   |                      ^^^ `<=>` is not a valid comparison operator, use `std::cmp::Ordering`
+
+error: aborting due to previous error
+