Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3b37d94

Browse files
committed
Add some tests
1 parent 4e3eb52 commit 3b37d94

File tree

6 files changed

+304
-0
lines changed

6 files changed

+304
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(unreachable_patterns)]
2+
3+
#[derive(PartialEq)]
4+
struct Opaque(i32);
5+
6+
impl Eq for Opaque {}
7+
8+
const FOO: Opaque = Opaque(42);
9+
10+
fn main() {
11+
match FOO {
12+
FOO => {},
13+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
14+
_ => {}
15+
//~^ ERROR unreachable pattern
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: to use a constant of type `Opaque` in a pattern, `Opaque` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/issue-78057.rs:12:9
3+
|
4+
LL | FOO => {},
5+
| ^^^
6+
7+
error: unreachable pattern
8+
--> $DIR/issue-78057.rs:14:9
9+
|
10+
LL | _ => {}
11+
| ^
12+
|
13+
note: the lint level is defined here
14+
--> $DIR/issue-78057.rs:1:9
15+
|
16+
LL | #![deny(unreachable_patterns)]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// This file tests the exhaustiveness algorithm on opaque constants. Most of the examples give
2+
// unnecessary warnings because const_to_pat.rs converts a constant pattern to a wildcard when the
3+
// constant is not allowed as a pattern. This is an edge case so we may not care to fix it.
4+
// See also https://github.com/rust-lang/rust/issues/78057
5+
6+
#![deny(unreachable_patterns)]
7+
8+
#[derive(PartialEq)]
9+
struct Foo(i32);
10+
impl Eq for Foo {}
11+
const FOO: Foo = Foo(42);
12+
const FOO_REF: &Foo = &Foo(42);
13+
const FOO_REF_REF: &&Foo = &&Foo(42);
14+
15+
#[derive(PartialEq)]
16+
struct Bar;
17+
impl Eq for Bar {}
18+
const BAR: Bar = Bar;
19+
20+
#[derive(PartialEq)]
21+
enum Baz {
22+
Baz1,
23+
Baz2
24+
}
25+
impl Eq for Baz {}
26+
const BAZ: Baz = Baz::Baz1;
27+
28+
type Quux = fn(usize, usize) -> usize;
29+
fn quux(a: usize, b: usize) -> usize { a + b }
30+
const QUUX: Quux = quux;
31+
32+
fn main() {
33+
match FOO {
34+
FOO => {}
35+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
36+
_ => {} // should not be emitting unreachable warning
37+
//~^ ERROR unreachable pattern
38+
}
39+
40+
match FOO_REF {
41+
FOO_REF => {}
42+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
43+
Foo(_) => {} // should not be emitting unreachable warning
44+
//~^ ERROR unreachable pattern
45+
}
46+
47+
// FIXME: this causes an ICE (https://github.com/rust-lang/rust/issues/78071)
48+
//match FOO_REF_REF {
49+
// FOO_REF_REF => {}
50+
// Foo(_) => {}
51+
//}
52+
53+
match BAR {
54+
Bar => {}
55+
BAR => {} // should not be emitting unreachable warning
56+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
57+
//~| ERROR unreachable pattern
58+
_ => {}
59+
//~^ ERROR unreachable pattern
60+
}
61+
62+
match BAR {
63+
BAR => {}
64+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
65+
Bar => {} // should not be emitting unreachable warning
66+
//~^ ERROR unreachable pattern
67+
_ => {}
68+
//~^ ERROR unreachable pattern
69+
}
70+
71+
match BAR {
72+
BAR => {}
73+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
74+
BAR => {} // should not be emitting unreachable warning
75+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
76+
//~| ERROR unreachable pattern
77+
_ => {} // should not be emitting unreachable warning
78+
//~^ ERROR unreachable pattern
79+
}
80+
81+
match BAZ {
82+
BAZ => {}
83+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
84+
Baz::Baz1 => {} // should not be emitting unreachable warning
85+
//~^ ERROR unreachable pattern
86+
_ => {}
87+
//~^ ERROR unreachable pattern
88+
}
89+
90+
match BAZ {
91+
Baz::Baz1 => {}
92+
BAZ => {}
93+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
94+
_ => {}
95+
//~^ ERROR unreachable pattern
96+
}
97+
98+
match BAZ {
99+
BAZ => {}
100+
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
101+
Baz::Baz2 => {} // should not be emitting unreachable warning
102+
//~^ ERROR unreachable pattern
103+
_ => {} // should not be emitting unreachable warning
104+
//~^ ERROR unreachable pattern
105+
}
106+
107+
match QUUX {
108+
QUUX => {}
109+
QUUX => {} // should not be emitting unreachable warning
110+
//~^ ERROR unreachable pattern
111+
_ => {}
112+
}
113+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/consts-opaque.rs:34:9
3+
|
4+
LL | FOO => {}
5+
| ^^^
6+
7+
error: unreachable pattern
8+
--> $DIR/consts-opaque.rs:36:9
9+
|
10+
LL | _ => {} // should not be emitting unreachable warning
11+
| ^
12+
|
13+
note: the lint level is defined here
14+
--> $DIR/consts-opaque.rs:6:9
15+
|
16+
LL | #![deny(unreachable_patterns)]
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
20+
--> $DIR/consts-opaque.rs:41:9
21+
|
22+
LL | FOO_REF => {}
23+
| ^^^^^^^
24+
25+
error: unreachable pattern
26+
--> $DIR/consts-opaque.rs:43:9
27+
|
28+
LL | Foo(_) => {} // should not be emitting unreachable warning
29+
| ^^^^^^
30+
31+
error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]`
32+
--> $DIR/consts-opaque.rs:55:9
33+
|
34+
LL | BAR => {} // should not be emitting unreachable warning
35+
| ^^^
36+
37+
error: unreachable pattern
38+
--> $DIR/consts-opaque.rs:55:9
39+
|
40+
LL | Bar => {}
41+
| --- matches any value
42+
LL | BAR => {} // should not be emitting unreachable warning
43+
| ^^^ unreachable pattern
44+
45+
error: unreachable pattern
46+
--> $DIR/consts-opaque.rs:58:9
47+
|
48+
LL | Bar => {}
49+
| --- matches any value
50+
...
51+
LL | _ => {}
52+
| ^ unreachable pattern
53+
54+
error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]`
55+
--> $DIR/consts-opaque.rs:63:9
56+
|
57+
LL | BAR => {}
58+
| ^^^
59+
60+
error: unreachable pattern
61+
--> $DIR/consts-opaque.rs:65:9
62+
|
63+
LL | Bar => {} // should not be emitting unreachable warning
64+
| ^^^
65+
66+
error: unreachable pattern
67+
--> $DIR/consts-opaque.rs:67:9
68+
|
69+
LL | Bar => {} // should not be emitting unreachable warning
70+
| --- matches any value
71+
LL |
72+
LL | _ => {}
73+
| ^ unreachable pattern
74+
75+
error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]`
76+
--> $DIR/consts-opaque.rs:72:9
77+
|
78+
LL | BAR => {}
79+
| ^^^
80+
81+
error: to use a constant of type `Bar` in a pattern, `Bar` must be annotated with `#[derive(PartialEq, Eq)]`
82+
--> $DIR/consts-opaque.rs:74:9
83+
|
84+
LL | BAR => {} // should not be emitting unreachable warning
85+
| ^^^
86+
87+
error: unreachable pattern
88+
--> $DIR/consts-opaque.rs:74:9
89+
|
90+
LL | BAR => {} // should not be emitting unreachable warning
91+
| ^^^
92+
93+
error: unreachable pattern
94+
--> $DIR/consts-opaque.rs:77:9
95+
|
96+
LL | _ => {} // should not be emitting unreachable warning
97+
| ^
98+
99+
error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]`
100+
--> $DIR/consts-opaque.rs:82:9
101+
|
102+
LL | BAZ => {}
103+
| ^^^
104+
105+
error: unreachable pattern
106+
--> $DIR/consts-opaque.rs:84:9
107+
|
108+
LL | Baz::Baz1 => {} // should not be emitting unreachable warning
109+
| ^^^^^^^^^
110+
111+
error: unreachable pattern
112+
--> $DIR/consts-opaque.rs:86:9
113+
|
114+
LL | _ => {}
115+
| ^
116+
117+
error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]`
118+
--> $DIR/consts-opaque.rs:92:9
119+
|
120+
LL | BAZ => {}
121+
| ^^^
122+
123+
error: unreachable pattern
124+
--> $DIR/consts-opaque.rs:94:9
125+
|
126+
LL | _ => {}
127+
| ^
128+
129+
error: to use a constant of type `Baz` in a pattern, `Baz` must be annotated with `#[derive(PartialEq, Eq)]`
130+
--> $DIR/consts-opaque.rs:99:9
131+
|
132+
LL | BAZ => {}
133+
| ^^^
134+
135+
error: unreachable pattern
136+
--> $DIR/consts-opaque.rs:101:9
137+
|
138+
LL | Baz::Baz2 => {} // should not be emitting unreachable warning
139+
| ^^^^^^^^^
140+
141+
error: unreachable pattern
142+
--> $DIR/consts-opaque.rs:103:9
143+
|
144+
LL | _ => {} // should not be emitting unreachable warning
145+
| ^
146+
147+
error: unreachable pattern
148+
--> $DIR/consts-opaque.rs:109:9
149+
|
150+
LL | QUUX => {} // should not be emitting unreachable warning
151+
| ^^^^
152+
153+
error: aborting due to 23 previous errors
154+

0 commit comments

Comments
 (0)