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

Gives a nicer error if the programmer uses the <=> operator. #89871

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 19 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes no sense to suggest that the spaceship operator be replaced with itself. Instead, use span_label.

Suggested change
.span_suggestion_short(
.span_label(

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()
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/spaceship-operator.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/spaceship-operator.stderr
Original file line number Diff line number Diff line change
@@ -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