Skip to content

Commit

Permalink
Rollup merge of #117892 - estebank:fat-arrow-typo, r=compiler-errors
Browse files Browse the repository at this point in the history
Detect more `=>` typos

Handle and recover `match expr { pat >= { arm } }`.
  • Loading branch information
matthiaskrgr authored Nov 16, 2023
2 parents a577704 + f830fe3 commit 92aba63
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ impl TokenKind {
match *self {
Comma => Some(vec![Dot, Lt, Semi]),
Semi => Some(vec![Colon, Comma]),
FatArrow => Some(vec![Eq, RArrow]),
Colon => Some(vec![Semi]),
FatArrow => Some(vec![Eq, RArrow, Ge, Gt]),
_ => None,
}
}
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2904,15 +2904,16 @@ impl<'a> Parser<'a> {
"=>",
Applicability::MachineApplicable,
);
err.emit();
this.bump();
} else if matches!(
(&this.prev_token.kind, &this.token.kind),
(token::DotDotEq, token::Gt)
) {
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
// so we suppress the error here
err.delay_as_bug();
if matches!(
(&this.prev_token.kind, &this.token.kind),
(token::DotDotEq, token::Gt)
) {
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
// so we suppress the error here
err.delay_as_bug();
} else {
err.emit();
}
this.bump();
} else {
return Err(err);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// run-rustfix
fn main() {
match 1 {
1 => {} //~ ERROR
_ => { let _: u16 = 2u16; } //~ ERROR
}
}
7 changes: 7 additions & 0 deletions tests/ui/parser/issues/recover-ge-as-fat-arrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// run-rustfix
fn main() {
match 1 {
1 >= {} //~ ERROR
_ => { let _: u16 = 2u8; } //~ ERROR
}
}
25 changes: 25 additions & 0 deletions tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error: expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`, found `>=`
--> $DIR/recover-ge-as-fat-arrow.rs:4:11
|
LL | 1 >= {}
| ^^
| |
| expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`
| help: use a fat arrow to start a match arm: `=>`

error[E0308]: mismatched types
--> $DIR/recover-ge-as-fat-arrow.rs:5:29
|
LL | _ => { let _: u16 = 2u8; }
| --- ^^^ expected `u16`, found `u8`
| |
| expected due to this
|
help: change the type of the numeric literal from `u8` to `u16`
|
LL | _ => { let _: u16 = 2u16; }
| ~~~

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 92aba63

Please sign in to comment.