Skip to content

Commit 7e4924b

Browse files
committed
Add tests
1 parent f9d52dc commit 7e4924b

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#![feature(never_type)]
2+
#![feature(exhaustive_patterns)]
3+
#![feature(type_alias_impl_trait)]
4+
#![feature(non_exhaustive_omitted_patterns_lint)]
5+
#![deny(unreachable_patterns)]
6+
// Test that the lint traversal handles opaques correctly
7+
#![deny(non_exhaustive_omitted_patterns)]
8+
9+
fn main() {}
10+
11+
#[derive(Copy, Clone)]
12+
enum Void {}
13+
14+
fn return_never_rpit(x: Void) -> impl Copy {
15+
if false {
16+
match return_never_rpit(x) {
17+
_ => {}
18+
}
19+
}
20+
x
21+
}
22+
fn friend_of_return_never_rpit(x: Void) {
23+
match return_never_rpit(x) {}
24+
//~^ ERROR non-empty
25+
}
26+
27+
type T = impl Copy;
28+
fn return_never_tait(x: Void) -> T {
29+
if false {
30+
match return_never_tait(x) {
31+
_ => {}
32+
}
33+
}
34+
x
35+
}
36+
fn friend_of_return_never_tait(x: Void) {
37+
match return_never_tait(x) {}
38+
//~^ ERROR non-empty
39+
}
40+
41+
fn option_never(x: Void) -> Option<impl Copy> {
42+
if false {
43+
match option_never(x) {
44+
None => {}
45+
Some(_) => {}
46+
}
47+
match option_never(x) {
48+
None => {}
49+
// FIXME: Unreachable not detected because `is_uninhabited` does not look into opaque
50+
// types.
51+
_ => {}
52+
}
53+
}
54+
Some(x)
55+
}
56+
57+
fn option_never2(x: Void) -> impl Copy {
58+
if false {
59+
match option_never2(x) {
60+
None => {}
61+
Some(_) => {} //~ ERROR unreachable
62+
}
63+
match option_never2(x) {
64+
None => {}
65+
_ => {} //~ ERROR unreachable
66+
}
67+
match option_never2(x) {
68+
None => {}
69+
}
70+
}
71+
Some(x)
72+
}
73+
74+
fn inner_never(x: Void) {
75+
type T = impl Copy;
76+
let y: T = x;
77+
match y {
78+
_ => {}
79+
}
80+
}
81+
82+
// This one caused ICE https://github.com/rust-lang/rust/issues/117100.
83+
fn inner_tuple() {
84+
type T = impl Copy;
85+
let foo: T = Some((1u32, 2u32));
86+
match foo {
87+
_ => {}
88+
Some((a, b)) => {} //~ ERROR unreachable
89+
}
90+
}
91+
92+
type U = impl Copy;
93+
fn unify_never(x: Void, u: U) -> U {
94+
if false {
95+
match u {
96+
_ => {}
97+
}
98+
}
99+
x
100+
}
101+
102+
type V = impl Copy;
103+
fn infer_in_match(x: Option<V>) {
104+
match x {
105+
None => {}
106+
Some((a, b)) => {}
107+
Some((mut x, mut y)) => {
108+
//~^ ERROR unreachable
109+
x = 42;
110+
y = "foo";
111+
}
112+
}
113+
}
114+
115+
type W = impl Copy;
116+
#[derive(Copy, Clone)]
117+
struct Rec<'a> {
118+
n: u32,
119+
w: Option<&'a W>,
120+
}
121+
fn recursive_opaque() -> W {
122+
if false {
123+
match recursive_opaque() {
124+
// Check for the ol' ICE when the type is recursively opaque.
125+
_ => {}
126+
Rec { n: 0, w: Some(Rec { n: 0, w: _ }) } => {} //~ ERROR unreachable
127+
}
128+
}
129+
let w: Option<&'static W> = None;
130+
Rec { n: 0, w }
131+
}
132+
133+
type X = impl Copy;
134+
struct SecretelyVoid(X);
135+
fn nested_empty_opaque(x: Void) -> X {
136+
if false {
137+
let opaque_void = nested_empty_opaque(x);
138+
let secretely_void = SecretelyVoid(opaque_void);
139+
match secretely_void {
140+
// FIXME: Unreachable not detected because `is_uninhabited` does not look into opaque
141+
// types.
142+
_ => {}
143+
}
144+
}
145+
x
146+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error: unreachable pattern
2+
--> $DIR/impl-trait.rs:61:13
3+
|
4+
LL | Some(_) => {}
5+
| ^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/impl-trait.rs:5:9
9+
|
10+
LL | #![deny(unreachable_patterns)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error: unreachable pattern
14+
--> $DIR/impl-trait.rs:65:13
15+
|
16+
LL | _ => {}
17+
| ^
18+
19+
error: unreachable pattern
20+
--> $DIR/impl-trait.rs:88:9
21+
|
22+
LL | _ => {}
23+
| - matches any value
24+
LL | Some((a, b)) => {}
25+
| ^^^^^^^^^^^^ unreachable pattern
26+
27+
error: unreachable pattern
28+
--> $DIR/impl-trait.rs:107:9
29+
|
30+
LL | Some((mut x, mut y)) => {
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
33+
error: unreachable pattern
34+
--> $DIR/impl-trait.rs:126:13
35+
|
36+
LL | _ => {}
37+
| - matches any value
38+
LL | Rec { n: 0, w: Some(Rec { n: 0, w: _ }) } => {}
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable pattern
40+
41+
error[E0004]: non-exhaustive patterns: type `impl Copy` is non-empty
42+
--> $DIR/impl-trait.rs:23:11
43+
|
44+
LL | match return_never_rpit(x) {}
45+
| ^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: the matched value is of type `impl Copy`
48+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
49+
|
50+
LL ~ match return_never_rpit(x) {
51+
LL + _ => todo!(),
52+
LL + }
53+
|
54+
55+
error[E0004]: non-exhaustive patterns: type `T` is non-empty
56+
--> $DIR/impl-trait.rs:37:11
57+
|
58+
LL | match return_never_tait(x) {}
59+
| ^^^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: the matched value is of type `T`
62+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern as shown
63+
|
64+
LL ~ match return_never_tait(x) {
65+
LL + _ => todo!(),
66+
LL + }
67+
|
68+
69+
error: aborting due to 7 previous errors
70+
71+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)