]> git.lizzy.rs Git - rust.git/blob - src/test/ui/exhaustive_integer_patterns.rs
Rollup merge of #57107 - mjbshaw:thread_local_test, r=nikomatsakis
[rust.git] / src / test / ui / exhaustive_integer_patterns.rs
1 #![feature(precise_pointer_size_matching)]
2 #![feature(exclusive_range_pattern)]
3
4 #![deny(unreachable_patterns)]
5
6 use std::{char, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128};
7
8 fn main() {
9     let x: u8 = 0;
10
11     // A single range covering the entire domain.
12     match x {
13         0 ..= 255 => {} // ok
14     }
15
16     // A combination of ranges and values.
17     // These are currently allowed to be overlapping.
18     match x {
19         0 ..= 32 => {}
20         33 => {}
21         34 .. 128 => {}
22         100 ..= 200 => {}
23         200 => {} //~ ERROR unreachable pattern
24         201 ..= 255 => {}
25     }
26
27     // An incomplete set of values.
28     match x { //~ ERROR non-exhaustive patterns
29         0 .. 128 => {}
30     }
31
32     // A more incomplete set of values.
33     match x { //~ ERROR non-exhaustive patterns
34         0 ..= 10 => {}
35         20 ..= 30 => {}
36         35 => {}
37         70 .. 255 => {}
38     }
39
40     let x: i8 = 0;
41     match x { //~ ERROR non-exhaustive patterns
42         -7 => {}
43         -5..=120 => {}
44         -2..=20 => {} //~ ERROR unreachable pattern
45         125 => {}
46     }
47
48     // Let's test other types too!
49     let c: char = '\u{0}';
50     match c {
51         '\u{0}' ..= char::MAX => {} // ok
52     }
53
54     // We can actually get away with just covering the
55     // following two ranges, which correspond to all
56     // valid Unicode Scalar Values.
57     match c {
58         '\u{0000}' ..= '\u{D7FF}' => {}
59         '\u{E000}' ..= '\u{10_FFFF}' => {}
60     }
61
62     match 0u16 {
63         0 ..= u16::MAX => {} // ok
64     }
65
66     match 0u32 {
67         0 ..= u32::MAX => {} // ok
68     }
69
70     match 0u64 {
71         0 ..= u64::MAX => {} // ok
72     }
73
74     match 0u128 {
75         0 ..= u128::MAX => {} // ok
76     }
77
78     match 0i8 {
79         -128 ..= 127 => {} // ok
80     }
81
82     match 0i8 { //~ ERROR non-exhaustive patterns
83         -127 ..= 127 => {}
84     }
85
86     match 0i16 {
87         i16::MIN ..= i16::MAX => {} // ok
88     }
89
90     match 0i16 { //~ ERROR non-exhaustive patterns
91         i16::MIN ..= -1 => {}
92         1 ..= i16::MAX => {}
93     }
94
95     match 0i32 {
96         i32::MIN ..= i32::MAX => {} // ok
97     }
98
99     match 0i64 {
100         i64::MIN ..= i64::MAX => {} // ok
101     }
102
103     match 0i128 {
104         i128::MIN ..= i128::MAX => {} // ok
105     }
106
107     // Make sure that guards don't factor into the exhaustiveness checks.
108     match 0u8 { //~ ERROR non-exhaustive patterns
109         0 .. 128 => {}
110         128 ..= 255 if true => {}
111     }
112
113     match 0u8 {
114         0 .. 128 => {}
115         128 ..= 255 if false => {}
116         128 ..= 255 => {} // ok, because previous arm was guarded
117     }
118
119     // Now things start getting a bit more interesting. Testing products!
120     match (0u8, Some(())) { //~ ERROR non-exhaustive patterns
121         (1, _) => {}
122         (_, None) => {}
123     }
124
125     match (0u8, true) { //~ ERROR non-exhaustive patterns
126         (0 ..= 125, false) => {}
127         (128 ..= 255, false) => {}
128         (0 ..= 255, true) => {}
129     }
130
131     match (0u8, true) { // ok
132         (0 ..= 125, false) => {}
133         (128 ..= 255, false) => {}
134         (0 ..= 255, true) => {}
135         (125 .. 128, false) => {}
136     }
137
138     match 0u8 { // ok
139         0 .. 2 => {}
140         1 ..= 2 => {}
141         _ => {}
142     }
143
144     const LIM: u128 = u128::MAX - 1;
145     match 0u128 { //~ ERROR non-exhaustive patterns
146         0 ..= LIM => {}
147     }
148
149     match 0u128 { //~ ERROR non-exhaustive patterns
150         0 ..= 4 => {}
151     }
152
153     match 0u128 { //~ ERROR non-exhaustive patterns
154         4 ..= u128::MAX => {}
155     }
156 }