Skip to content

Commit f194c9b

Browse files
committed
Recover on invalid operators <> and <=>
1 parent 0fb1c37 commit f194c9b

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

compiler/rustc_parse/src/parser/expr.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ impl<'a> Parser<'a> {
213213
}
214214
}
215215

216+
// Look for JS' `===` and `!==` and recover
216217
if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual)
217218
&& self.token.kind == token::Eq
218219
&& self.prev_token.span.hi() == self.token.span.lo()
219220
{
220-
// Look for JS' `===` and `!==` and recover 😇
221221
let sp = op.span.to(self.token.span);
222222
let sugg = match op.node {
223223
AssocOp::Equal => "==",
@@ -235,6 +235,38 @@ impl<'a> Parser<'a> {
235235
self.bump();
236236
}
237237

238+
// Look for PHP's `<>` and recover
239+
if op.node == AssocOp::Less
240+
&& self.token.kind == token::Gt
241+
&& self.prev_token.span.hi() == self.token.span.lo()
242+
{
243+
let sp = op.span.to(self.token.span);
244+
self.struct_span_err(sp, "invalid comparison operator `<>`")
245+
.span_suggestion_short(
246+
sp,
247+
"`<>` is not a valid comparison operator, use `!=`",
248+
"!=".to_string(),
249+
Applicability::MachineApplicable,
250+
)
251+
.emit();
252+
self.bump();
253+
}
254+
255+
// Look for C++'s `<=>` and recover
256+
if op.node == AssocOp::LessEqual
257+
&& self.token.kind == token::Gt
258+
&& self.prev_token.span.hi() == self.token.span.lo()
259+
{
260+
let sp = op.span.to(self.token.span);
261+
self.struct_span_err(sp, "invalid comparison operator `<=>`")
262+
.span_label(
263+
sp,
264+
"`<=>` is not a valid comparison operator, use `std::cmp::Ordering`",
265+
)
266+
.emit();
267+
self.bump();
268+
}
269+
238270
let op = op.node;
239271
// Special cases:
240272
if op == AssocOp::As {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!("{}", 1 <> 2);
3+
//~^ERROR invalid comparison operator `<>`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: invalid comparison operator `<>`
2+
--> $DIR/less-than-greater-than.rs:2:22
3+
|
4+
LL | println!("{}", 1 <> 2);
5+
| ^^ help: `<>` is not a valid comparison operator, use `!=`
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
println!("{}", 1 <=> 2);
3+
//~^ERROR invalid comparison operator `<=>`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: invalid comparison operator `<=>`
2+
--> $DIR/spaceship.rs:2:22
3+
|
4+
LL | println!("{}", 1 <=> 2);
5+
| ^^^ `<=>` is not a valid comparison operator, use `std::cmp::Ordering`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)