Skip to content

Commit 3dc2494

Browse files
authored
Rollup merge of #98240 - GKFX:unexpected-big-arrow, r=Dylan-DPC
Write help for unexpected `=>`. Fixes #98128 #98128 discusses what to do with the typo `=>` vs `>=`, or efforts to write a JavaScript-syntax closure in Rust (`(a, b) => (a + b)`). Both manifest as an unexpected `token::FatArrow` so the error message describes both. I think that only those who are very new to programming would actually need to be told how to spell `>=`, but I could understand someone misreading the existing error message and not realising what they'd typed. The opposite of this is: ``` (a, b) =< (a + b); ^ expected one of `>` or `as` ``` which I think would be significantly more complex to detect because `=<` isn't a token, and less beneficial as I'm not aware of a language which actually uses that syntax for something.
2 parents 17cfc77 + 04f9957 commit 3dc2494

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ impl<'a> Parser<'a> {
606606
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Gt)))
607607
{
608608
err.span_label(self.prev_token.span, "maybe try to close unmatched angle bracket");
609+
} else if self.token == token::FatArrow
610+
&& expected.iter().any(|tok| matches!(tok, TokenType::Operator))
611+
{
612+
err.help("closures are written `|a, b| a + b` and greater-than-or-equal is `>=`.");
609613
}
610614

611615
let sp = if self.token == token::Eof {

src/test/ui/asm/x86_64/parse-error.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, or an operator, found `=>`
5757
|
5858
LL | asm!("{}", in(reg) foo => bar);
5959
| ^^ expected one of 7 possible tokens
60+
|
61+
= help: closures are written `|a, b| a + b` and greater-than-or-equal is `>=`.
6062

6163
error: expected a path for argument to `sym`
6264
--> $DIR/parse-error.rs:31:24

src/test/ui/missing/missing-comma-in-match.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | &None => 1
55
| - help: missing a comma here to end this `match` arm
66
LL | &Some(2) => { 3 }
77
| ^^ expected one of `,`, `.`, `?`, `}`, or an operator
8+
|
9+
= help: closures are written `|a, b| a + b` and greater-than-or-equal is `>=`.
810

911
error: aborting due to previous error
1012

0 commit comments

Comments
 (0)