Skip to content

Commit 1f0fb03

Browse files
committed
Add a bunch more tests.
1 parent 214f977 commit 1f0fb03

15 files changed

+506
-5
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ pub enum PartiallyInhabitedVariants {
2323
Tuple(u8),
2424
#[non_exhaustive] Struct { x: ! }
2525
}
26+
27+
pub struct IndirectUninhabitedEnum(UninhabitedEnum);
28+
29+
pub struct IndirectUninhabitedStruct(UninhabitedStruct);
30+
31+
pub struct IndirectUninhabitedTupleStruct(UninhabitedTupleStruct);
32+
33+
pub struct IndirectUninhabitedVariants(UninhabitedVariants);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// aux-build:uninhabited.rs
2+
#![feature(never_type)]
3+
4+
extern crate uninhabited;
5+
6+
use uninhabited::{
7+
IndirectUninhabitedEnum,
8+
IndirectUninhabitedStruct,
9+
IndirectUninhabitedTupleStruct,
10+
IndirectUninhabitedVariants,
11+
};
12+
13+
struct A;
14+
15+
// This test checks that an empty match on a non-exhaustive uninhabited type through a level of
16+
// indirection from an extern crate will not compile.
17+
18+
fn cannot_empty_match_on_empty_enum_to_anything(x: IndirectUninhabitedEnum) -> A {
19+
match x {} //~ ERROR non-exhaustive patterns
20+
}
21+
22+
fn cannot_empty_match_on_empty_struct_to_anything(x: IndirectUninhabitedStruct) -> A {
23+
match x {} //~ ERROR non-exhaustive patterns
24+
}
25+
26+
fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: IndirectUninhabitedTupleStruct) -> A {
27+
match x {} //~ ERROR non-exhaustive patterns
28+
}
29+
30+
fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(
31+
x: IndirectUninhabitedVariants,
32+
) -> A {
33+
match x {} //~ ERROR non-exhaustive patterns
34+
}
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `uninhabited::IndirectUninhabitedEnum` is not handled
2+
--> $DIR/indirect_match.rs:19: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+
9+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedStruct` of type `uninhabited::IndirectUninhabitedStruct` is not handled
10+
--> $DIR/indirect_match.rs:23:11
11+
|
12+
LL | match x {}
13+
| ^
14+
|
15+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
16+
17+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedTupleStruct` of type `uninhabited::IndirectUninhabitedTupleStruct` is not handled
18+
--> $DIR/indirect_match.rs:27:11
19+
|
20+
LL | match x {}
21+
| ^
22+
|
23+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
24+
25+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `uninhabited::IndirectUninhabitedVariants` is not handled
26+
--> $DIR/indirect_match.rs:33:11
27+
|
28+
LL | match x {}
29+
| ^
30+
|
31+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0004`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![feature(never_type)]
2+
#![feature(non_exhaustive)]
3+
4+
#[non_exhaustive]
5+
pub enum UninhabitedEnum {
6+
}
7+
8+
#[non_exhaustive]
9+
pub struct UninhabitedStruct {
10+
_priv: !,
11+
}
12+
13+
#[non_exhaustive]
14+
pub struct UninhabitedTupleStruct(!);
15+
16+
pub enum UninhabitedVariants {
17+
#[non_exhaustive] Tuple(!),
18+
#[non_exhaustive] Struct { x: ! }
19+
}
20+
21+
pub struct IndirectUninhabitedEnum(UninhabitedEnum);
22+
23+
pub struct IndirectUninhabitedStruct(UninhabitedStruct);
24+
25+
pub struct IndirectUninhabitedTupleStruct(UninhabitedTupleStruct);
26+
27+
pub struct IndirectUninhabitedVariants(UninhabitedVariants);
28+
29+
struct A;
30+
31+
// This test checks that an empty match on a non-exhaustive uninhabited type through a level of
32+
// indirection from the defining crate will not compile without `#![feature(exhaustive_patterns)]`.
33+
34+
fn cannot_empty_match_on_empty_enum_to_anything(x: IndirectUninhabitedEnum) -> A {
35+
match x {} //~ ERROR non-exhaustive patterns
36+
}
37+
38+
fn cannot_empty_match_on_empty_struct_to_anything(x: IndirectUninhabitedStruct) -> A {
39+
match x {} //~ ERROR non-exhaustive patterns
40+
}
41+
42+
fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: IndirectUninhabitedTupleStruct) -> A {
43+
match x {} //~ ERROR non-exhaustive patterns
44+
}
45+
46+
fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(
47+
x: IndirectUninhabitedVariants,
48+
) -> A {
49+
match x {} //~ ERROR non-exhaustive patterns
50+
}
51+
52+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `IndirectUninhabitedEnum` is not handled
2+
--> $DIR/indirect_match_same_crate.rs:35:11
3+
|
4+
LL | pub struct IndirectUninhabitedEnum(UninhabitedEnum);
5+
| ----------------------------------------------------
6+
| | |
7+
| | variant not covered
8+
| `IndirectUninhabitedEnum` defined here
9+
...
10+
LL | match x {}
11+
| ^
12+
|
13+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
14+
15+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedStruct` of type `IndirectUninhabitedStruct` is not handled
16+
--> $DIR/indirect_match_same_crate.rs:39:11
17+
|
18+
LL | pub struct IndirectUninhabitedStruct(UninhabitedStruct);
19+
| --------------------------------------------------------
20+
| | |
21+
| | variant not covered
22+
| `IndirectUninhabitedStruct` defined here
23+
...
24+
LL | match x {}
25+
| ^
26+
|
27+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
28+
29+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedTupleStruct` of type `IndirectUninhabitedTupleStruct` is not handled
30+
--> $DIR/indirect_match_same_crate.rs:43:11
31+
|
32+
LL | pub struct IndirectUninhabitedTupleStruct(UninhabitedTupleStruct);
33+
| ------------------------------------------------------------------
34+
| | |
35+
| | variant not covered
36+
| `IndirectUninhabitedTupleStruct` defined here
37+
...
38+
LL | match x {}
39+
| ^
40+
|
41+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
42+
43+
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `IndirectUninhabitedVariants` is not handled
44+
--> $DIR/indirect_match_same_crate.rs:49:11
45+
|
46+
LL | pub struct IndirectUninhabitedVariants(UninhabitedVariants);
47+
| ------------------------------------------------------------
48+
| | |
49+
| | variant not covered
50+
| `IndirectUninhabitedVariants` defined here
51+
...
52+
LL | match x {}
53+
| ^
54+
|
55+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
56+
57+
error: aborting due to 4 previous errors
58+
59+
For more information about this error, try `rustc --explain E0004`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// aux-build:uninhabited.rs
2+
#![deny(unreachable_patterns)]
3+
#![feature(exhaustive_patterns)]
4+
#![feature(never_type)]
5+
6+
extern crate uninhabited;
7+
8+
use uninhabited::{
9+
IndirectUninhabitedEnum,
10+
IndirectUninhabitedStruct,
11+
IndirectUninhabitedTupleStruct,
12+
IndirectUninhabitedVariants,
13+
};
14+
15+
struct A;
16+
17+
// This test checks that an empty match on a non-exhaustive uninhabited type through a level of
18+
// indirection from an extern crate will not compile. In particular, this enables the
19+
// `exhaustive_patterns` feature as this can change the branch used in the compiler to determine
20+
// this.
21+
22+
fn cannot_empty_match_on_empty_enum_to_anything(x: IndirectUninhabitedEnum) -> A {
23+
match x {} //~ ERROR non-exhaustive patterns
24+
}
25+
26+
fn cannot_empty_match_on_empty_struct_to_anything(x: IndirectUninhabitedStruct) -> A {
27+
match x {} //~ ERROR non-exhaustive patterns
28+
}
29+
30+
fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: IndirectUninhabitedTupleStruct) -> A {
31+
match x {} //~ ERROR non-exhaustive patterns
32+
}
33+
34+
fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(
35+
x: IndirectUninhabitedVariants,
36+
) -> A {
37+
match x {} //~ ERROR non-exhaustive patterns
38+
}
39+
40+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedEnum` is non-empty
2+
--> $DIR/indirect_match_with_exhaustive_patterns.rs:23: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+
9+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedStruct` is non-empty
10+
--> $DIR/indirect_match_with_exhaustive_patterns.rs:27:11
11+
|
12+
LL | match x {}
13+
| ^
14+
|
15+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
16+
17+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedTupleStruct` is non-empty
18+
--> $DIR/indirect_match_with_exhaustive_patterns.rs:31:11
19+
|
20+
LL | match x {}
21+
| ^
22+
|
23+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
24+
25+
error[E0004]: non-exhaustive patterns: type `uninhabited::IndirectUninhabitedVariants` is non-empty
26+
--> $DIR/indirect_match_with_exhaustive_patterns.rs:37:11
27+
|
28+
LL | match x {}
29+
| ^
30+
|
31+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0004`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// compile-pass
2+
// skip-codegen
3+
#![deny(unreachable_patterns)]
4+
#![feature(exhaustive_patterns)]
5+
#![feature(never_type)]
6+
#![feature(non_exhaustive)]
7+
8+
#[non_exhaustive]
9+
pub enum UninhabitedEnum {
10+
}
11+
12+
#[non_exhaustive]
13+
pub struct UninhabitedStruct {
14+
_priv: !,
15+
}
16+
17+
#[non_exhaustive]
18+
pub struct UninhabitedTupleStruct(!);
19+
20+
pub enum UninhabitedVariants {
21+
#[non_exhaustive] Tuple(!),
22+
#[non_exhaustive] Struct { x: ! }
23+
}
24+
25+
pub struct IndirectUninhabitedEnum(UninhabitedEnum);
26+
27+
pub struct IndirectUninhabitedStruct(UninhabitedStruct);
28+
29+
pub struct IndirectUninhabitedTupleStruct(UninhabitedTupleStruct);
30+
31+
pub struct IndirectUninhabitedVariants(UninhabitedVariants);
32+
33+
struct A;
34+
35+
// This test checks that an empty match on a non-exhaustive uninhabited type from the defining crate
36+
// will compile. In particular, this enables the `exhaustive_patterns` feature as this can
37+
// change the branch used in the compiler to determine this.
38+
// Codegen is skipped because tests with long names can cause issues on Windows CI, see #60648.
39+
40+
fn cannot_empty_match_on_empty_enum_to_anything(x: IndirectUninhabitedEnum) -> A {
41+
match x {}
42+
}
43+
44+
fn cannot_empty_match_on_empty_struct_to_anything(x: IndirectUninhabitedStruct) -> A {
45+
match x {}
46+
}
47+
48+
fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: IndirectUninhabitedTupleStruct) -> A {
49+
match x {}
50+
}
51+
52+
fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(
53+
x: IndirectUninhabitedVariants,
54+
) -> A {
55+
match x {}
56+
}
57+
58+
fn main() {}

src/test/ui/rfc-2008-non-exhaustive/uninhabited/match.rs

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ extern crate uninhabited;
55

66
use uninhabited::{
77
UninhabitedEnum,
8+
UninhabitedStruct,
9+
UninhabitedTupleStruct,
10+
UninhabitedVariants,
811
};
912

1013
struct A;
@@ -16,4 +19,16 @@ fn cannot_empty_match_on_empty_enum_to_anything(x: UninhabitedEnum) -> A {
1619
match x {} //~ ERROR non-exhaustive patterns
1720
}
1821

22+
fn cannot_empty_match_on_empty_struct_to_anything(x: UninhabitedStruct) -> A {
23+
match x {} //~ ERROR non-exhaustive patterns
24+
}
25+
26+
fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: UninhabitedTupleStruct) -> A {
27+
match x {} //~ ERROR non-exhaustive patterns
28+
}
29+
30+
fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(x: UninhabitedVariants) -> A {
31+
match x {} //~ ERROR non-exhaustive patterns
32+
}
33+
1934
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
error[E0004]: non-exhaustive patterns: type `uninhabited::UninhabitedEnum` is non-empty
2-
--> $DIR/match.rs:16:11
2+
--> $DIR/match.rs:19:11
33
|
44
LL | match x {}
55
| ^
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error: aborting due to previous error
9+
error[E0004]: non-exhaustive patterns: pattern `UninhabitedStruct` of type `uninhabited::UninhabitedStruct` is not handled
10+
--> $DIR/match.rs:23:11
11+
|
12+
LL | match x {}
13+
| ^
14+
|
15+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
16+
17+
error[E0004]: non-exhaustive patterns: pattern `UninhabitedTupleStruct` of type `uninhabited::UninhabitedTupleStruct` is not handled
18+
--> $DIR/match.rs:27:11
19+
|
20+
LL | match x {}
21+
| ^
22+
|
23+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
24+
25+
error[E0004]: non-exhaustive patterns: multiple patterns of type `uninhabited::UninhabitedVariants` are not handled
26+
--> $DIR/match.rs:31:11
27+
|
28+
LL | match x {}
29+
| ^
30+
|
31+
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
32+
33+
error: aborting due to 4 previous errors
1034

1135
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)