From 480edb1151dcf2ef15e4f9501a60b9d71d63cac7 Mon Sep 17 00:00:00 2001 From: moonheart08 Date: Wed, 13 Oct 2021 19:50:26 -0500 Subject: [PATCH 1/2] Add an error for C++'s <=> --- compiler/rustc_parse/src/parser/expr.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 3d29d30502109..439b22aad2e86 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -213,6 +213,25 @@ impl<'a> Parser<'a> { } } + if op.node == AssocOp::LessEqual + && self.token.kind == token::Gt + && self.prev_token.span.hi() == self.token.span.lo() + { + // Look for C++'s `<=>` + let sp = op.span.to(self.token.span); + self.struct_span_err(sp, &format!("invalid comparison operator `<=>`")) + .span_suggestion_short( + sp, + &format!( + "`<=>` is not a valid comparison operator, use std::cmp::Ordering" + ), + "<=>".to_string(), + Applicability::Unspecified, + ) + .emit(); + self.bump(); + } + if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual) && self.token.kind == token::Eq && self.prev_token.span.hi() == self.token.span.lo() From 4f1cb6e95e198fc2e4f408a69518c00b880655d7 Mon Sep 17 00:00:00 2001 From: moonheart08 Date: Wed, 13 Oct 2021 20:21:06 -0500 Subject: [PATCH 2/2] Tests for spaceship operator --- src/test/ui/spaceship-operator.rs | 4 ++++ src/test/ui/spaceship-operator.stderr | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/test/ui/spaceship-operator.rs create mode 100644 src/test/ui/spaceship-operator.stderr diff --git a/src/test/ui/spaceship-operator.rs b/src/test/ui/spaceship-operator.rs new file mode 100644 index 0000000000000..a65f9389625fc --- /dev/null +++ b/src/test/ui/spaceship-operator.rs @@ -0,0 +1,4 @@ +fn main() { + println!("{}", 1 <=> 2); + //~^ERROR invalid comparison operator `<=>` +} diff --git a/src/test/ui/spaceship-operator.stderr b/src/test/ui/spaceship-operator.stderr new file mode 100644 index 0000000000000..f32fb380eeb77 --- /dev/null +++ b/src/test/ui/spaceship-operator.stderr @@ -0,0 +1,8 @@ +error: invalid comparison operator `<=>` + --> $DIR/spaceship-operator.rs:2:22 + | +LL | println!("{}", 1 <=> 2); + | ^^^ help: `<=>` is not a valid comparison operator, use std::cmp::Ordering + +error: aborting due to previous error +