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 on invalid operators <> and <=> #91597

Merged
merged 1 commit into from
Dec 15, 2021
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
34 changes: 33 additions & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ impl<'a> Parser<'a> {
}
}

// 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 => "==",
Expand All @@ -235,6 +235,38 @@ impl<'a> Parser<'a> {
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 {
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/operator-recovery/less-than-greater-than.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("{}", 1 <> 2);
//~^ERROR invalid comparison operator `<>`
}
8 changes: 8 additions & 0 deletions src/test/ui/operator-recovery/less-than-greater-than.stderr
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions src/test/ui/operator-recovery/spaceship.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("{}", 1 <=> 2);
//~^ERROR invalid comparison operator `<=>`
}
8 changes: 8 additions & 0 deletions src/test/ui/operator-recovery/spaceship.stderr
Original file line number Diff line number Diff line change
@@ -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