]> git.lizzy.rs Git - rust.git/blob - src/test/ui/exhaustive_integer_patterns.rs
Add semi-exhaustive tests for exhaustiveness
[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: `128u8...255u8` not covered
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     match '\u{0}' {
59         '\u{0}' ..= char::MAX => {} // ok
60     }
61
62     match 0usize {
63         0 ..= usize::MAX => {} // ok
64     }
65
66     match 0u16 {
67         0 ..= u16::MAX => {} // ok
68     }
69
70     match 0u32 {
71         0 ..= u32::MAX => {} // ok
72     }
73
74     match 0u64 {
75         0 ..= u64::MAX => {} // ok
76     }
77
78     match 0u128 {
79         0 ..= u128::MAX => {} // ok
80     }
81
82     match 0isize {
83         isize::MIN ..= isize::MAX => {} // ok
84     }
85
86     match 0i8 {
87         -128..=127 => {} // ok
88     }
89
90     match 0i16 {
91         i16::MIN ..= i16::MAX => {} // ok
92     }
93
94     match 0i32 {
95         i32::MIN ..= i32::MAX => {} // ok
96     }
97
98     match 0i64 {
99         i64::MIN ..= i64::MAX => {} // ok
100     }
101
102     match 0i128 {
103         i128::MIN ..= i128::MAX => {} // ok
104     }
105 }