]> git.proxmox.com Git - rustc.git/blob - src/test/ui/rfc-2008-non-exhaustive/enum.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / test / ui / rfc-2008-non-exhaustive / enum.rs
1 // aux-build:enums.rs
2 extern crate enums;
3
4 use enums::{EmptyNonExhaustiveEnum, NonExhaustiveEnum};
5
6 fn empty(x: EmptyNonExhaustiveEnum) {
7 match x {} //~ ERROR type `enums::EmptyNonExhaustiveEnum` is non-empty
8 match x {
9 _ => {}, // ok
10 }
11 }
12
13 fn main() {
14 let enum_unit = NonExhaustiveEnum::Unit;
15
16 match enum_unit {
17 //~^ ERROR non-exhaustive patterns: `_` not covered [E0004]
18 NonExhaustiveEnum::Unit => "first",
19 NonExhaustiveEnum::Tuple(_) => "second",
20 NonExhaustiveEnum::Struct { .. } => "third"
21 };
22
23 match enum_unit {};
24 //~^ ERROR non-exhaustive patterns: `_` not covered [E0004]
25
26 // Everything below this is expected to compile successfully.
27
28 let enum_unit = NonExhaustiveEnum::Unit;
29
30 match enum_unit {
31 NonExhaustiveEnum::Unit => 1,
32 NonExhaustiveEnum::Tuple(_) => 2,
33 // This particular arm tests that a enum marked as non-exhaustive
34 // will not error if its variants are matched exhaustively.
35 NonExhaustiveEnum::Struct { field } => field,
36 _ => 0 // no error with wildcard
37 };
38
39 match enum_unit {
40 _ => "no error with only wildcard"
41 };
42
43 // #53549: Check that variant constructors can still be called normally.
44 match NonExhaustiveEnum::Unit {
45 NonExhaustiveEnum::Unit => {},
46 _ => {}
47 };
48
49 match NonExhaustiveEnum::Tuple(2) {
50 NonExhaustiveEnum::Tuple(2) => {},
51 _ => {}
52 };
53
54 match (NonExhaustiveEnum::Unit {}) {
55 NonExhaustiveEnum::Unit {} => {},
56 _ => {}
57 };
58
59 match (NonExhaustiveEnum::Tuple { 0: 2 }) {
60 NonExhaustiveEnum::Tuple { 0: 2 } => {},
61 _ => {}
62 };
63
64 match (NonExhaustiveEnum::Struct { field: 2 }) {
65 NonExhaustiveEnum::Struct { field: 2 } => {},
66 _ => {}
67 };
68
69 }