Rollup merge of #117892 - estebank:fat-arrow-typo, r=compiler-errors

Detect more `=>` typos

Handle and recover `match expr { pat >= { arm } }`.
This commit is contained in:
Matthias Krüger 2023-11-17 00:41:22 +01:00 committed by GitHub
commit 92aba63d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 10 deletions

View File

@ -388,7 +388,8 @@ impl TokenKind {
match *self { match *self {
Comma => Some(vec![Dot, Lt, Semi]), Comma => Some(vec![Dot, Lt, Semi]),
Semi => Some(vec![Colon, Comma]), Semi => Some(vec![Colon, Comma]),
FatArrow => Some(vec![Eq, RArrow]), Colon => Some(vec![Semi]),
FatArrow => Some(vec![Eq, RArrow, Ge, Gt]),
_ => None, _ => None,
} }
} }

View File

@ -2904,15 +2904,16 @@ impl<'a> Parser<'a> {
"=>", "=>",
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
err.emit(); if matches!(
this.bump();
} else if matches!(
(&this.prev_token.kind, &this.token.kind), (&this.prev_token.kind, &this.token.kind),
(token::DotDotEq, token::Gt) (token::DotDotEq, token::Gt)
) { ) {
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`, // `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
// so we suppress the error here // so we suppress the error here
err.delay_as_bug(); err.delay_as_bug();
} else {
err.emit();
}
this.bump(); this.bump();
} else { } else {
return Err(err); return Err(err);

View File

@ -0,0 +1,7 @@
// run-rustfix
fn main() {
match 1 {
1 => {} //~ ERROR
_ => { let _: u16 = 2u16; } //~ ERROR
}
}

View File

@ -0,0 +1,7 @@
// run-rustfix
fn main() {
match 1 {
1 >= {} //~ ERROR
_ => { let _: u16 = 2u8; } //~ ERROR
}
}

View File

@ -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`.