about summary refs log tree commit diff
path: root/tests/ui/pattern
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-01-15 21:24:13 +0000
committerbors <bors@rust-lang.org>2024-01-15 21:24:13 +0000
commit714b29a17ff5fa727c794bbb60bfd335f8e75d42 (patch)
tree3ccf9791c9f9d2ea627134c0e41c8f61114e5d5f /tests/ui/pattern
parentbfcc027a751595ba290c554f47907eaa3779f798 (diff)
parent5ccd29d6f0c8e8f678bb05adfd8482de0ecde0c2 (diff)
downloadrust-714b29a17ff5fa727c794bbb60bfd335f8e75d42.tar.gz
rust-714b29a17ff5fa727c794bbb60bfd335f8e75d42.zip
Auto merge of #119610 - Nadrieril:never_pattern_bindings, r=compiler-errors
never patterns: Check bindings wrt never patterns

Never patterns:
- Shouldn't contain bindings since they never match anything;
- Don't count when checking that or-patterns have consistent bindings.

r? `@compiler-errors`
Diffstat (limited to 'tests/ui/pattern')
-rw-r--r--tests/ui/pattern/never_patterns.rs28
-rw-r--r--tests/ui/pattern/never_patterns.stderr58
2 files changed, 13 insertions, 73 deletions
diff --git a/tests/ui/pattern/never_patterns.rs b/tests/ui/pattern/never_patterns.rs
index 915f3e75e7b..8f44f8a6559 100644
--- a/tests/ui/pattern/never_patterns.rs
+++ b/tests/ui/pattern/never_patterns.rs
@@ -7,12 +7,9 @@ fn main() {}
 
 // The classic use for empty types.
 fn safe_unwrap_result<T>(res: Result<T, Void>) {
-    let Ok(_x) = res;
-    // FIXME(never_patterns): These should be allowed
+    let Ok(_x) = res; //~ ERROR refutable pattern in local binding
     let (Ok(_x) | Err(!)) = &res;
-    //~^ ERROR: is not bound in all patterns
     let (Ok(_x) | Err(&!)) = res.as_ref();
-    //~^ ERROR: is not bound in all patterns
 }
 
 // Check we only accept `!` where we want to.
@@ -74,26 +71,3 @@ fn never_pattern_location(void: Void) {
         Some(&(_, !)),
     }
 }
-
-fn never_and_bindings() {
-    let x: Result<bool, &(u32, Void)> = Ok(false);
-
-    // FIXME(never_patterns): Never patterns in or-patterns don't need to share the same bindings.
-    match x {
-        Ok(_x) | Err(&!) => {}
-        //~^ ERROR: is not bound in all patterns
-    }
-    let (Ok(_x) | Err(&!)) = x;
-    //~^ ERROR: is not bound in all patterns
-
-    // FIXME(never_patterns): A never pattern mustn't have bindings.
-    match x {
-        Ok(_) => {}
-        Err(&(_b, !)),
-    }
-    match x {
-        Ok(_a) | Err(&(_b, !)) => {}
-        //~^ ERROR: is not bound in all patterns
-        //~| ERROR: is not bound in all patterns
-    }
-}
diff --git a/tests/ui/pattern/never_patterns.stderr b/tests/ui/pattern/never_patterns.stderr
index 11e50debfd3..20eeb01cf71 100644
--- a/tests/ui/pattern/never_patterns.stderr
+++ b/tests/ui/pattern/never_patterns.stderr
@@ -1,51 +1,17 @@
-error[E0408]: variable `_x` is not bound in all patterns
-  --> $DIR/never_patterns.rs:12:19
+error[E0005]: refutable pattern in local binding
+  --> $DIR/never_patterns.rs:10:9
    |
-LL |     let (Ok(_x) | Err(!)) = &res;
-   |             --    ^^^^^^ pattern doesn't bind `_x`
-   |             |
-   |             variable not in all patterns
-
-error[E0408]: variable `_x` is not bound in all patterns
-  --> $DIR/never_patterns.rs:14:19
-   |
-LL |     let (Ok(_x) | Err(&!)) = res.as_ref();
-   |             --    ^^^^^^^ pattern doesn't bind `_x`
-   |             |
-   |             variable not in all patterns
-
-error[E0408]: variable `_x` is not bound in all patterns
-  --> $DIR/never_patterns.rs:83:18
-   |
-LL |         Ok(_x) | Err(&!) => {}
-   |            --    ^^^^^^^ pattern doesn't bind `_x`
-   |            |
-   |            variable not in all patterns
-
-error[E0408]: variable `_x` is not bound in all patterns
-  --> $DIR/never_patterns.rs:86:19
-   |
-LL |     let (Ok(_x) | Err(&!)) = x;
-   |             --    ^^^^^^^ pattern doesn't bind `_x`
-   |             |
-   |             variable not in all patterns
-
-error[E0408]: variable `_b` is not bound in all patterns
-  --> $DIR/never_patterns.rs:95:9
+LL |     let Ok(_x) = res;
+   |         ^^^^^^ pattern `Err(_)` not covered
    |
-LL |         Ok(_a) | Err(&(_b, !)) => {}
-   |         ^^^^^^         -- variable not in all patterns
-   |         |
-   |         pattern doesn't bind `_b`
-
-error[E0408]: variable `_a` is not bound in all patterns
-  --> $DIR/never_patterns.rs:95:18
+   = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
+   = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
+   = note: the matched value is of type `Result<T, Void>`
+help: you might want to use `let else` to handle the variant that isn't matched
    |
-LL |         Ok(_a) | Err(&(_b, !)) => {}
-   |            --    ^^^^^^^^^^^^^ pattern doesn't bind `_a`
-   |            |
-   |            variable not in all patterns
+LL |     let Ok(_x) = res else { todo!() };
+   |                      ++++++++++++++++
 
-error: aborting due to 6 previous errors
+error: aborting due to 1 previous error
 
-For more information about this error, try `rustc --explain E0408`.
+For more information about this error, try `rustc --explain E0005`.