Skip to content

Commit 922310d

Browse files
committed
Add tests
1 parent 063d74f commit 922310d

File tree

9 files changed

+343
-3
lines changed

9 files changed

+343
-3
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![feature(never_type)]
2+
#![feature(exhaustive_patterns)]
3+
#![deny(unreachable_patterns)]
4+
enum Foo {}
5+
6+
struct NonEmptyStruct(bool);
7+
enum NonEmptyEnum1 {
8+
Foo(bool),
9+
}
10+
enum NonEmptyEnum2 {
11+
Foo(bool),
12+
Bar,
13+
}
14+
enum NonEmptyEnum5 {
15+
V1, V2, V3, V4, V5,
16+
}
17+
18+
fn foo(x: Foo) {
19+
match x {} // ok
20+
match x {
21+
_ => {}, //~ ERROR unreachable pattern
22+
}
23+
}
24+
25+
fn main() {
26+
// `exhaustive_patterns` is not on, so uninhabited branches are not detected as unreachable.
27+
match None::<!> {
28+
None => {}
29+
Some(_) => {} //~ ERROR unreachable pattern
30+
}
31+
match None::<Foo> {
32+
None => {}
33+
Some(_) => {} //~ ERROR unreachable pattern
34+
}
35+
36+
match 0u8 {}
37+
//~^ ERROR type `u8` is non-empty
38+
match NonEmptyStruct(true) {}
39+
//~^ ERROR type `NonEmptyStruct` is non-empty
40+
match NonEmptyEnum1::Foo(true) {}
41+
//~^ ERROR type `NonEmptyEnum1` is non-empty
42+
match NonEmptyEnum2::Foo(true) {}
43+
//~^ ERROR type `NonEmptyEnum2` is non-empty
44+
match NonEmptyEnum5::V1 {}
45+
//~^ ERROR type `NonEmptyEnum5` is non-empty
46+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
error: unreachable pattern
2+
--> $DIR/match-empty-exhaustive_patterns.rs:21:9
3+
|
4+
LL | _ => {},
5+
| ^
6+
|
7+
note: lint level defined here
8+
--> $DIR/match-empty-exhaustive_patterns.rs:3:9
9+
|
10+
LL | #![deny(unreachable_patterns)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: unreachable pattern
14+
--> $DIR/match-empty-exhaustive_patterns.rs:29:9
15+
|
16+
LL | Some(_) => {}
17+
| ^^^^^^^
18+
19+
error: unreachable pattern
20+
--> $DIR/match-empty-exhaustive_patterns.rs:33:9
21+
|
22+
LL | Some(_) => {}
23+
| ^^^^^^^
24+
25+
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
26+
--> $DIR/match-empty-exhaustive_patterns.rs:36:11
27+
|
28+
LL | match 0u8 {}
29+
| ^^^
30+
|
31+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
32+
33+
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
34+
--> $DIR/match-empty-exhaustive_patterns.rs:38:11
35+
|
36+
LL | match NonEmptyStruct(true) {}
37+
| ^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
40+
41+
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum1` is non-empty
42+
--> $DIR/match-empty-exhaustive_patterns.rs:40:11
43+
|
44+
LL | match NonEmptyEnum1::Foo(true) {}
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
48+
49+
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum2` is non-empty
50+
--> $DIR/match-empty-exhaustive_patterns.rs:42:11
51+
|
52+
LL | match NonEmptyEnum2::Foo(true) {}
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^
54+
|
55+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
56+
57+
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum5` is non-empty
58+
--> $DIR/match-empty-exhaustive_patterns.rs:44:11
59+
|
60+
LL | match NonEmptyEnum5::V1 {}
61+
| ^^^^^^^^^^^^^^^^^
62+
|
63+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
64+
65+
error: aborting due to 8 previous errors
66+
67+
For more information about this error, try `rustc --explain E0004`.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![feature(never_type)]
2+
#![deny(unreachable_patterns)]
3+
enum Foo {}
4+
5+
struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here
6+
enum NonEmptyEnum1 { //~ `NonEmptyEnum1` defined here
7+
Foo(bool), //~ variant not covered
8+
}
9+
enum NonEmptyEnum2 { //~ `NonEmptyEnum2` defined here
10+
Foo(bool), //~ variant not covered
11+
Bar, //~ variant not covered
12+
}
13+
enum NonEmptyEnum5 { //~ `NonEmptyEnum5` defined here
14+
V1, V2, V3, V4, V5,
15+
}
16+
17+
fn foo1(x: Foo) {
18+
match x {} // ok
19+
}
20+
21+
fn foo2(x: Foo) {
22+
match x {
23+
_ => {}, // FIXME: should be unreachable
24+
}
25+
}
26+
27+
fn main() {
28+
// `exhaustive_patterns` is not on, so uninhabited branches are not detected as unreachable.
29+
match None::<!> {
30+
None => {}
31+
Some(_) => {}
32+
}
33+
match None::<Foo> {
34+
None => {}
35+
Some(_) => {}
36+
}
37+
38+
match 0u8 {}
39+
//~^ ERROR type `u8` is non-empty
40+
match NonEmptyStruct(true) {}
41+
//~^ ERROR pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
42+
match NonEmptyEnum1::Foo(true) {}
43+
//~^ ERROR pattern `Foo` of type `NonEmptyEnum1` is not handled
44+
match NonEmptyEnum2::Foo(true) {}
45+
//~^ ERROR multiple patterns of type `NonEmptyEnum2` are not handled
46+
match NonEmptyEnum5::V1 {}
47+
//~^ ERROR type `NonEmptyEnum5` is non-empty
48+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
error[E0004]: non-exhaustive patterns: type `u8` is non-empty
2+
--> $DIR/match-empty.rs:38:11
3+
|
4+
LL | match 0u8 {}
5+
| ^^^
6+
|
7+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
8+
9+
error[E0004]: non-exhaustive patterns: pattern `NonEmptyStruct` of type `NonEmptyStruct` is not handled
10+
--> $DIR/match-empty.rs:40:11
11+
|
12+
LL | struct NonEmptyStruct(bool);
13+
| ----------------------------
14+
| | |
15+
| | variant not covered
16+
| `NonEmptyStruct` defined here
17+
...
18+
LL | match NonEmptyStruct(true) {}
19+
| ^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
22+
23+
error[E0004]: non-exhaustive patterns: pattern `Foo` of type `NonEmptyEnum1` is not handled
24+
--> $DIR/match-empty.rs:42:11
25+
|
26+
LL | / enum NonEmptyEnum1 {
27+
LL | | Foo(bool),
28+
| | --- variant not covered
29+
LL | | }
30+
| |_- `NonEmptyEnum1` defined here
31+
...
32+
LL | match NonEmptyEnum1::Foo(true) {}
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
36+
37+
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonEmptyEnum2` are not handled
38+
--> $DIR/match-empty.rs:44:11
39+
|
40+
LL | / enum NonEmptyEnum2 {
41+
LL | | Foo(bool),
42+
| | --- variant not covered
43+
LL | | Bar,
44+
| | --- variant not covered
45+
LL | | }
46+
| |_- `NonEmptyEnum2` defined here
47+
...
48+
LL | match NonEmptyEnum2::Foo(true) {}
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^
50+
|
51+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
52+
53+
error[E0004]: non-exhaustive patterns: type `NonEmptyEnum5` is non-empty
54+
--> $DIR/match-empty.rs:46:11
55+
|
56+
LL | / enum NonEmptyEnum5 {
57+
LL | | V1, V2, V3, V4, V5,
58+
LL | | }
59+
| |_- `NonEmptyEnum5` defined here
60+
...
61+
LL | match NonEmptyEnum5::V1 {}
62+
| ^^^^^^^^^^^^^^^^^
63+
|
64+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
65+
66+
error: aborting due to 5 previous errors
67+
68+
For more information about this error, try `rustc --explain E0004`.

src/test/ui/rfc-2008-non-exhaustive/auxiliary/enums.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ pub enum NonExhaustiveEnum {
66
Tuple(u32),
77
Struct { field: u32 }
88
}
9+
10+
#[non_exhaustive]
11+
pub enum EmptyNonExhaustiveEnum {}

src/test/ui/rfc-2008-non-exhaustive/enum.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// aux-build:enums.rs
22
extern crate enums;
33

4-
use enums::NonExhaustiveEnum;
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+
}
512

613
fn main() {
714
let enum_unit = NonExhaustiveEnum::Unit;
@@ -13,6 +20,9 @@ fn main() {
1320
NonExhaustiveEnum::Struct { .. } => "third"
1421
};
1522

23+
match enum_unit {};
24+
//~^ ERROR non-exhaustive patterns: multiple patterns of type `enums::NonExhaustiveEnum` are not handled [E0004]
25+
1626
// Everything below this is expected to compile successfully.
1727

1828
let enum_unit = NonExhaustiveEnum::Unit;
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1+
error[E0004]: non-exhaustive patterns: type `enums::EmptyNonExhaustiveEnum` is non-empty
2+
--> $DIR/enum.rs:7:11
3+
|
4+
LL | match x {}
5+
| ^
6+
|
7+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
8+
19
error[E0004]: non-exhaustive patterns: `_` not covered
2-
--> $DIR/enum.rs:9:11
10+
--> $DIR/enum.rs:16:11
311
|
412
LL | match enum_unit {
513
| ^^^^^^^^^ pattern `_` not covered
614
|
715
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
816

9-
error: aborting due to previous error
17+
error[E0004]: non-exhaustive patterns: multiple patterns of type `enums::NonExhaustiveEnum` are not handled
18+
--> $DIR/enum.rs:23:11
19+
|
20+
LL | match enum_unit {};
21+
| ^^^^^^^^^
22+
|
23+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
24+
25+
error: aborting due to 3 previous errors
1026

1127
For more information about this error, try `rustc --explain E0004`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(unreachable_patterns)]
2+
3+
#[non_exhaustive]
4+
pub enum NonExhaustiveEnum {
5+
Unit,
6+
//~^ variant not covered
7+
Tuple(u32),
8+
//~^ variant not covered
9+
Struct { field: u32 }
10+
//~^ variant not covered
11+
}
12+
13+
pub enum NormalEnum {
14+
Unit,
15+
//~^ variant not covered
16+
Tuple(u32),
17+
//~^ variant not covered
18+
Struct { field: u32 }
19+
//~^ variant not covered
20+
}
21+
22+
#[non_exhaustive]
23+
pub enum EmptyNonExhaustiveEnum {}
24+
25+
fn empty_non_exhaustive(x: EmptyNonExhaustiveEnum) {
26+
match x {}
27+
match x {
28+
_ => {} // FIXME: should be unreachable
29+
}
30+
}
31+
32+
fn main() {
33+
match NonExhaustiveEnum::Unit {}
34+
//~^ ERROR multiple patterns of type `NonExhaustiveEnum` are not handled [E0004]
35+
match NormalEnum::Unit {}
36+
//~^ ERROR multiple patterns of type `NormalEnum` are not handled [E0004]
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0004]: non-exhaustive patterns: multiple patterns of type `NonExhaustiveEnum` are not handled
2+
--> $DIR/enum_same_crate_empty_match.rs:33:11
3+
|
4+
LL | / pub enum NonExhaustiveEnum {
5+
LL | | Unit,
6+
| | ---- variant not covered
7+
LL | |
8+
LL | | Tuple(u32),
9+
| | ----- variant not covered
10+
LL | |
11+
LL | | Struct { field: u32 }
12+
| | ------ variant not covered
13+
LL | |
14+
LL | | }
15+
| |_- `NonExhaustiveEnum` defined here
16+
...
17+
LL | match NonExhaustiveEnum::Unit {}
18+
| ^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
21+
22+
error[E0004]: non-exhaustive patterns: multiple patterns of type `NormalEnum` are not handled
23+
--> $DIR/enum_same_crate_empty_match.rs:35:11
24+
|
25+
LL | / pub enum NormalEnum {
26+
LL | | Unit,
27+
| | ---- variant not covered
28+
LL | |
29+
LL | | Tuple(u32),
30+
| | ----- variant not covered
31+
LL | |
32+
LL | | Struct { field: u32 }
33+
| | ------ variant not covered
34+
LL | |
35+
LL | | }
36+
| |_- `NormalEnum` defined here
37+
...
38+
LL | match NormalEnum::Unit {}
39+
| ^^^^^^^^^^^^^^^^
40+
|
41+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
42+
43+
error: aborting due to 2 previous errors
44+
45+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)