File: consts-opaque.rs

package info (click to toggle)
rustc-web 1.78.0%2Bdfsg1-2~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,245,420 kB
  • sloc: xml: 147,985; javascript: 18,022; sh: 11,083; python: 10,265; ansic: 6,172; cpp: 5,023; asm: 4,390; makefile: 4,269
file content (146 lines) | stat: -rw-r--r-- 3,460 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// This file tests the exhaustiveness algorithm on opaque constants. Most of the examples give
// unnecessary warnings because const_to_pat.rs converts a constant pattern to a wildcard when the
// constant is not allowed as a pattern. This is an edge case so we may not care to fix it.
// See also https://github.com/rust-lang/rust/issues/78057

#![deny(unreachable_patterns)]

#[derive(PartialEq)]
struct Foo(i32);
impl Eq for Foo {}
const FOO: Foo = Foo(42);
const FOO_REF: &Foo = &Foo(42);
const FOO_REF_REF: &&Foo = &&Foo(42);

#[derive(PartialEq)]
struct Bar;
impl Eq for Bar {}
const BAR: Bar = Bar;

#[derive(PartialEq)]
enum Baz {
    Baz1,
    Baz2,
}
impl Eq for Baz {}
const BAZ: Baz = Baz::Baz1;

#[rustfmt::skip]
fn main() {
    match FOO {
        FOO => {}
        _ => {}
    }

    match FOO_REF {
        FOO_REF => {}
        Foo(_) => {}
    }

    // This used to cause an ICE (https://github.com/rust-lang/rust/issues/78071)
    match FOO_REF_REF {
        FOO_REF_REF => {}
        Foo(_) => {}
    }

    match BAR {
        Bar => {}
        BAR => {}
        //~^ ERROR unreachable pattern
        _ => {}
        //~^ ERROR unreachable pattern
    }

    match BAR {
        BAR => {}
        Bar => {}
        //~^ ERROR unreachable pattern
        _ => {}
        //~^ ERROR unreachable pattern
    }

    match BAR {
        BAR => {}
        BAR => {} // should not be emitting unreachable warning
        //~^ ERROR unreachable pattern
        _ => {} // should not be emitting unreachable warning
        //~^ ERROR unreachable pattern
    }

    match BAZ {
        BAZ => {}
        Baz::Baz1 => {} // should not be emitting unreachable warning
        //~^ ERROR unreachable pattern
        _ => {}
    }

    match BAZ {
        Baz::Baz1 => {}
        BAZ => {}
        //~^ ERROR unreachable pattern
        _ => {}
    }

    match BAZ {
        BAZ => {}
        Baz::Baz2 => {}
        _ => {} // should not be emitting unreachable warning
        //~^ ERROR unreachable pattern
    }

    type Quux = fn(usize, usize) -> usize;
    fn quux(a: usize, b: usize) -> usize { a + b }
    const QUUX: Quux = quux;

    match QUUX {
        QUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        QUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        _ => {}
    }

    #[derive(PartialEq, Eq)]
    struct Wrap<T>(T);
    const WRAPQUUX: Wrap<Quux> = Wrap(quux);

    match WRAPQUUX {
        WRAPQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        WRAPQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        Wrap(_) => {}
    }

    match WRAPQUUX {
        Wrap(_) => {}
        WRAPQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
    }

    match WRAPQUUX {
        Wrap(_) => {}
    }

    match WRAPQUUX {
        //~^ ERROR: non-exhaustive patterns: `Wrap(_)` not covered
        WRAPQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
    }

    #[derive(PartialEq, Eq)]
    enum WhoKnows<T> {
        Yay(T),
        Nope,
    };
    const WHOKNOWSQUUX: WhoKnows<Quux> = WhoKnows::Yay(quux);

    match WHOKNOWSQUUX {
        WHOKNOWSQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        WhoKnows::Yay(_) => {}
        WHOKNOWSQUUX => {} //~WARN behave unpredictably
        //~| previously accepted
        WhoKnows::Nope => {}
    }
}