]> git.lizzy.rs Git - rust.git/blob - tests/ui/closures/2229_closure_analysis/match/non-exhaustive-match.rs
Rollup merge of #107248 - erikdesjardins:addrspace, r=oli-obk
[rust.git] / tests / ui / closures / 2229_closure_analysis / match / non-exhaustive-match.rs
1 // edition:2021
2
3 // aux-build:match_non_exhaustive_lib.rs
4
5 /* The error message for non-exhaustive matches on non-local enums
6  * marked as non-exhaustive should mention the fact that the enum
7  * is marked as non-exhaustive (issue #85227).
8  */
9
10 // Ignore non_exhaustive in the same crate
11 #[non_exhaustive]
12 enum L1 { A, B }
13 enum L2 { C }
14
15 extern crate match_non_exhaustive_lib;
16 use match_non_exhaustive_lib::{E1, E2, E3, E4};
17
18 fn foo() -> (L1, L2) {todo!()}
19 fn bar() -> (E1, E2, E3, E4) {todo!()}
20
21 fn main() {
22     let (l1, l2) = foo();
23     // No error for enums defined in this crate
24     let _a = || { match l1 { L1::A => (), L1::B => () } };
25     // (except if the match is already non-exhaustive)
26     let _b = || { match l1 { L1::A => () } };
27     //~^ ERROR: non-exhaustive patterns: `L1::B` not covered [E0004]
28
29     // l2 should not be captured as it is a non-exhaustive SingleVariant
30     // defined in this crate
31     let _c = || { match l2 { L2::C => (), _ => () }  };
32     let mut mut_l2 = l2;
33     _c();
34
35     // E1 is not visibly uninhabited from here
36     let (e1, e2, e3, e4) = bar();
37     let _d = || { match e1 {} };
38     //~^ ERROR: non-exhaustive patterns: type `E1` is non-empty [E0004]
39     let _e = || { match e2 { E2::A => (), E2::B => () } };
40     //~^ ERROR: non-exhaustive patterns: `_` not covered [E0004]
41     let _f = || { match e2 { E2::A => (), E2::B => (), _ => () }  };
42
43     // e3 should be captured as it is a non-exhaustive SingleVariant
44     // defined in another crate
45     let _g = || { match e3 { E3::C => (), _ => () }  };
46     let mut mut_e3 = e3;
47     //~^ ERROR: cannot move out of `e3` because it is borrowed
48     _g();
49
50     // e4 should not be captured as it is a SingleVariant
51     let _h = || { match e4 { E4::D => (), _ => () }  };
52     let mut mut_e4 = e4;
53     _h();
54 }