]>
Commit | Line | Data |
---|---|---|
1 | #![feature(or_patterns)] | |
2 | #![deny(unreachable_patterns)] | |
3 | ||
4 | // We wrap patterns in a tuple because top-level or-patterns were special-cased. | |
5 | fn main() { | |
6 | match (0u8,) { | |
7 | (1 | 2,) => {} | |
8 | (1,) => {} //~ ERROR unreachable pattern | |
9 | _ => {} | |
10 | } | |
11 | match (0u8,) { | |
12 | (1 | 2,) => {} | |
13 | (2,) => {} //~ ERROR unreachable pattern | |
14 | _ => {} | |
15 | } | |
16 | match (0u8,) { | |
17 | (1,) => {} | |
18 | (2,) => {} | |
19 | (1 | 2,) => {} //~ ERROR unreachable pattern | |
20 | _ => {} | |
21 | } | |
22 | match (0u8, 0u8) { | |
23 | (1 | 2, 3 | 4) => {} | |
24 | (1, 3) => {} //~ ERROR unreachable pattern | |
25 | (1, 4) => {} //~ ERROR unreachable pattern | |
26 | (2, 4) => {} //~ ERROR unreachable pattern | |
27 | (2 | 1, 4) => {} //~ ERROR unreachable pattern | |
28 | (1, 5 | 6) => {} | |
29 | (1, 4 | 5) => {} //~ ERROR unreachable pattern | |
30 | _ => {} | |
31 | } | |
32 | match (true, true) { | |
33 | (false | true, false | true) => (), | |
34 | } | |
35 | match (Some(0u8),) { | |
36 | (None | Some(1 | 2),) => {} | |
37 | (Some(1),) => {} //~ ERROR unreachable pattern | |
38 | (None,) => {} //~ ERROR unreachable pattern | |
39 | _ => {} | |
40 | } | |
41 | match ((0u8,),) { | |
42 | ((1 | 2,) | (3 | 4,),) => {} | |
43 | ((1..=4,),) => {} //~ ERROR unreachable pattern | |
44 | _ => {} | |
45 | } | |
46 | ||
47 | match (0,) { | |
48 | (1 | 1,) => {} //~ ERROR unreachable | |
49 | _ => {} | |
50 | } | |
51 | match 0 { | |
52 | (0 | 1) | 1 => {} //~ ERROR unreachable | |
53 | _ => {} | |
54 | } | |
55 | match 0 { | |
56 | // We get two errors because recursive or-pattern expansion means we don't notice the two | |
57 | // errors span a whole pattern. This could be better but doesn't matter much | |
58 | 0 | (0 | 0) => {} | |
59 | //~^ ERROR unreachable | |
60 | //~| ERROR unreachable | |
61 | _ => {} | |
62 | } | |
63 | match None { | |
64 | // There is only one error that correctly points to the whole subpattern | |
65 | Some(0) | | |
66 | Some( //~ ERROR unreachable | |
67 | 0 | 0) => {} | |
68 | _ => {} | |
69 | } | |
70 | match [0; 2] { | |
71 | [0 | |
72 | | 0 //~ ERROR unreachable | |
73 | , 0 | |
74 | | 0] => {} //~ ERROR unreachable | |
75 | _ => {} | |
76 | } | |
77 | match &[][..] { | |
78 | [0] => {} | |
79 | [0, _] => {} | |
80 | [0, _, _] => {} | |
81 | [1, ..] => {} | |
82 | [1 //~ ERROR unreachable | |
83 | | 2, ..] => {} | |
84 | _ => {} | |
85 | } | |
86 | match &[][..] { | |
87 | [true] => {} | |
88 | [true | false, ..] => {} | |
89 | _ => {} | |
90 | } | |
91 | match &[][..] { | |
92 | [false] => {} | |
93 | [true, ..] => {} | |
94 | [true //~ ERROR unreachable | |
95 | | false, ..] => {} | |
96 | _ => {} | |
97 | } | |
98 | match (true, None) { | |
99 | (true, Some(_)) => {} | |
100 | (false, Some(true)) => {} | |
101 | (true | false, None | Some(true //~ ERROR unreachable | |
102 | | false)) => {} | |
103 | } | |
104 | macro_rules! t_or_f { | |
105 | () => { | |
106 | (true //~ ERROR unreachable | |
107 | | false) | |
108 | }; | |
109 | } | |
110 | match (true, None) { | |
111 | (true, Some(_)) => {} | |
112 | (false, Some(true)) => {} | |
113 | (true | false, None | Some(t_or_f!())) => {} | |
114 | } | |
115 | match Some(0) { | |
116 | Some(0) => {} | |
117 | Some(0 //~ ERROR unreachable | |
118 | | 1) => {} | |
119 | _ => {} | |
120 | } | |
121 | ||
122 | // A subpattern that is only unreachable in one branch is overall reachable. | |
123 | match (true, true) { | |
124 | (true, true) => {} | |
125 | (false | true, false | true) => {} | |
126 | } | |
127 | match (true, true) { | |
128 | (true, true) => {} | |
129 | (false, false) => {} | |
130 | (false | true, false | true) => {} | |
131 | } | |
132 | // https://github.com/rust-lang/rust/issues/76836 | |
133 | match None { | |
134 | Some(false) => {} | |
135 | None | Some(true | |
136 | | false) => {} //~ ERROR unreachable | |
137 | } | |
138 | ||
139 | // A subpattern that is unreachable in all branches is overall unreachable. | |
140 | match (true, true) { | |
141 | (false, true) => {} | |
142 | (true, true) => {} | |
143 | (false | true, false | |
144 | | true) => {} //~ ERROR unreachable | |
145 | } | |
146 | match (true, true) { | |
147 | (true, false) => {} | |
148 | (true, true) => {} | |
149 | (false | |
150 | | true, //~ ERROR unreachable | |
151 | false | true) => {} | |
152 | } | |
153 | } |