]> git.lizzy.rs Git - rust.git/blob - src/test/ui/pattern/usefulness/consts-opaque.rs
Auto merge of #78682 - glandium:issue78471, r=lcnr
[rust.git] / src / test / ui / pattern / usefulness / consts-opaque.rs
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     // This used to cause an ICE (https://github.com/rust-lang/rust/issues/78071)
48     match FOO_REF_REF {
49         FOO_REF_REF => {}
50         //~^ WARNING must be annotated with `#[derive(PartialEq, Eq)]`
51         //~| WARNING this was previously accepted by the compiler but is being phased out
52         Foo(_) => {}
53     }
54
55     match BAR {
56         Bar => {}
57         BAR => {} // should not be emitting unreachable warning
58         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
59         //~| ERROR unreachable pattern
60         _ => {}
61         //~^ ERROR unreachable pattern
62     }
63
64     match BAR {
65         BAR => {}
66         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
67         Bar => {} // should not be emitting unreachable warning
68         //~^ ERROR unreachable pattern
69         _ => {}
70         //~^ ERROR unreachable pattern
71     }
72
73     match BAR {
74         BAR => {}
75         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
76         BAR => {} // should not be emitting unreachable warning
77         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
78         //~| ERROR unreachable pattern
79         _ => {} // should not be emitting unreachable warning
80         //~^ ERROR unreachable pattern
81     }
82
83     match BAZ {
84         BAZ => {}
85         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
86         Baz::Baz1 => {} // should not be emitting unreachable warning
87         //~^ ERROR unreachable pattern
88         _ => {}
89         //~^ ERROR unreachable pattern
90     }
91
92     match BAZ {
93         Baz::Baz1 => {}
94         BAZ => {}
95         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
96         _ => {}
97         //~^ ERROR unreachable pattern
98     }
99
100     match BAZ {
101         BAZ => {}
102         //~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
103         Baz::Baz2 => {} // should not be emitting unreachable warning
104         //~^ ERROR unreachable pattern
105         _ => {} // should not be emitting unreachable warning
106         //~^ ERROR unreachable pattern
107     }
108
109     match QUUX {
110         QUUX => {}
111         QUUX => {}
112         _ => {}
113     }
114 }