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