Rollup merge of #91597 - r00ster91:lessthangreaterthan, r=oli-obk
Recover on invalid operators `<>` and `<=>` Thanks to #89871 for showing me how to do this. Next, I think it'd be nice to recover on `<=>` too, like #89871 intended, if this even works.
This commit is contained in:
commit
4b37cfc50c
@ -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 => "==",
|
||||
@ -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 {
|
||||
|
4
src/test/ui/operator-recovery/less-than-greater-than.rs
Normal file
4
src/test/ui/operator-recovery/less-than-greater-than.rs
Normal file
@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println!("{}", 1 <> 2);
|
||||
//~^ERROR invalid comparison operator `<>`
|
||||
}
|
@ -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
src/test/ui/operator-recovery/spaceship.rs
Normal file
4
src/test/ui/operator-recovery/spaceship.rs
Normal file
@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
println!("{}", 1 <=> 2);
|
||||
//~^ERROR invalid comparison operator `<=>`
|
||||
}
|
8
src/test/ui/operator-recovery/spaceship.stderr
Normal file
8
src/test/ui/operator-recovery/spaceship.stderr
Normal file
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user