]> git.lizzy.rs Git - rust.git/blob - src/test/ui/pattern/usefulness/integer-ranges/reachability.rs
Rollup merge of #80298 - PankajChaudhary5:PankajChaudhary, r=GuillaumeGomez
[rust.git] / src / test / ui / pattern / usefulness / integer-ranges / reachability.rs
1 #![feature(exclusive_range_pattern)]
2 #![allow(overlapping_range_endpoints)]
3 #![deny(unreachable_patterns)]
4
5 macro_rules! m {
6     ($s:expr, $t1:pat, $t2:pat) => {
7         match $s {
8             $t1 => {}
9             $t2 => {}
10             _ => {}
11         }
12     }
13 }
14
15 fn main() {
16     m!(0u8, 42, 41);
17     m!(0u8, 42, 42); //~ ERROR unreachable pattern
18     m!(0u8, 42, 43);
19
20     m!(0u8, 20..=30, 19);
21     m!(0u8, 20..=30, 20); //~ ERROR unreachable pattern
22     m!(0u8, 20..=30, 21); //~ ERROR unreachable pattern
23     m!(0u8, 20..=30, 25); //~ ERROR unreachable pattern
24     m!(0u8, 20..=30, 29); //~ ERROR unreachable pattern
25     m!(0u8, 20..=30, 30); //~ ERROR unreachable pattern
26     m!(0u8, 20..=30, 31);
27     m!(0u8, 20..30, 19);
28     m!(0u8, 20..30, 20); //~ ERROR unreachable pattern
29     m!(0u8, 20..30, 21); //~ ERROR unreachable pattern
30     m!(0u8, 20..30, 25); //~ ERROR unreachable pattern
31     m!(0u8, 20..30, 29); //~ ERROR unreachable pattern
32     m!(0u8, 20..30, 30);
33     m!(0u8, 20..30, 31);
34
35     m!(0u8, 20..=30, 20..=30); //~ ERROR unreachable pattern
36     m!(0u8, 20.. 30, 20.. 30); //~ ERROR unreachable pattern
37     m!(0u8, 20..=30, 20.. 30); //~ ERROR unreachable pattern
38     m!(0u8, 20..=30, 19..=30);
39     m!(0u8, 20..=30, 21..=30); //~ ERROR unreachable pattern
40     m!(0u8, 20..=30, 20..=29); //~ ERROR unreachable pattern
41     m!(0u8, 20..=30, 20..=31);
42     m!('a', 'A'..='z', 'a'..='z'); //~ ERROR unreachable pattern
43
44     match 0u8 {
45         5 => {},
46         6 => {},
47         7 => {},
48         8 => {},
49         5..=8 => {}, //~ ERROR unreachable pattern
50         _ => {},
51     }
52     match 0u8 {
53         0..10 => {},
54         10..20 => {},
55         5..15 => {}, //~ ERROR unreachable pattern
56         _ => {},
57     }
58     match 0u8 {
59         0..10 => {},
60         10..20 => {},
61         20..30 => {},
62         5..25 => {}, //~ ERROR unreachable pattern
63         _ => {},
64     }
65     match 0u8 {
66         0..10 => {},
67         10 => {},
68         11..=23 => {},
69         19..30 => {},
70         5..25 => {}, //~ ERROR unreachable pattern
71         _ => {},
72     }
73     match 0usize {
74         0..10 => {},
75         10..20 => {},
76         5..15 => {}, //~ ERROR unreachable pattern
77         _ => {},
78     }
79     // Chars between '\u{D7FF}' and '\u{E000}' are invalid even though ranges that contain them are
80     // allowed.
81     match 'a' {
82         _ => {},
83         '\u{D7FF}'..='\u{E000}' => {}, //~ ERROR unreachable pattern
84     }
85     match 'a' {
86         '\u{0}'..='\u{D7FF}' => {},
87         '\u{E000}'..='\u{10_FFFF}' => {},
88         '\u{D7FF}'..='\u{E000}' => {}, // FIXME should be unreachable
89     }
90
91     match (0u8, true) {
92         (0..=255, false) => {}
93         (0..=255, true) => {} // ok
94     }
95     match (true, 0u8) {
96         (false, 0..=255) => {}
97         (true, 0..=255) => {} // ok
98     }
99
100     const FOO: i32 = 42;
101     const BAR: &i32 = &42;
102     match &0 {
103         &42 => {}
104         &FOO => {} //~ ERROR unreachable pattern
105         BAR => {} //~ ERROR unreachable pattern
106         _ => {}
107     }
108     // Regression test, see https://github.com/rust-lang/rust/pull/66326#issuecomment-552889933
109     match &0 {
110         BAR => {} // ok
111         _ => {}
112     }
113 }