about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-11-17 00:41:22 +0100
committerGitHub <noreply@github.com>2023-11-17 00:41:22 +0100
commit92aba63d6b46dbcf50b544c6cef8b85d8713aa09 (patch)
treee2f3283684fe23e4431ec20c5e6fd416a711638b
parenta57770440f1ebe5b992551d3bcc489ae211908d4 (diff)
parentf830fe313ba8b23fe882589ba8dcbbd5739137e8 (diff)
downloadrust-92aba63d6b46dbcf50b544c6cef8b85d8713aa09.tar.gz
rust-92aba63d6b46dbcf50b544c6cef8b85d8713aa09.zip
Rollup merge of #117892 - estebank:fat-arrow-typo, r=compiler-errors
Detect more `=>` typos

Handle and recover `match expr { pat >= { arm } }`.
-rw-r--r--compiler/rustc_ast/src/token.rs3
-rw-r--r--compiler/rustc_parse/src/parser/expr.rs19
-rw-r--r--tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed7
-rw-r--r--tests/ui/parser/issues/recover-ge-as-fat-arrow.rs7
-rw-r--r--tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr25
5 files changed, 51 insertions, 10 deletions
diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs
index a6ee93e8a6b..707238e6c02 100644
--- a/compiler/rustc_ast/src/token.rs
+++ b/compiler/rustc_ast/src/token.rs
@@ -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,
         }
     }
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs
index 235b28b6e26..bfd7e8ef4d0 100644
--- a/compiler/rustc_parse/src/parser/expr.rs
+++ b/compiler/rustc_parse/src/parser/expr.rs
@@ -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);
diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed b/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed
new file mode 100644
index 00000000000..7b73dfb02df
--- /dev/null
+++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.fixed
@@ -0,0 +1,7 @@
+// run-rustfix
+fn main() {
+    match 1 {
+        1 => {} //~ ERROR
+        _ => { let _: u16 = 2u16; } //~ ERROR
+    }
+}
diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs b/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs
new file mode 100644
index 00000000000..92143fcf3f7
--- /dev/null
+++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.rs
@@ -0,0 +1,7 @@
+// run-rustfix
+fn main() {
+    match 1 {
+        1 >= {} //~ ERROR
+        _ => { let _: u16 = 2u8; } //~ ERROR
+    }
+}
diff --git a/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr b/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr
new file mode 100644
index 00000000000..2df5cca24f0
--- /dev/null
+++ b/tests/ui/parser/issues/recover-ge-as-fat-arrow.stderr
@@ -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`.