]> git.proxmox.com Git - rustc.git/blame - src/test/ui/pattern/usefulness/integer-ranges/exhaustiveness.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / test / ui / pattern / usefulness / integer-ranges / exhaustiveness.rs
CommitLineData
fc512014 1#![feature(exclusive_range_pattern)]
fc512014
XL
2#![allow(overlapping_range_endpoints)]
3#![deny(unreachable_patterns)]
4
5macro_rules! m {
6 ($s:expr, $($t:tt)+) => {
7 match $s { $($t)+ => {} }
8 }
9}
10
11macro_rules! test_int {
12 ($s:expr, $min:path, $max:path) => {
13 m!($s, $min..=$max);
14 m!($s, $min..5 | 5..=$max);
15 m!($s, $min..=4 | 5..=$max);
16 m!($s, $min..$max | $max);
17 m!(($s, true), ($min..5, true) | (5..=$max, true) | ($min..=$max, false));
18 }
19}
20
21fn main() {
22 test_int!(0u8, u8::MIN, u8::MAX);
23 test_int!(0u16, u16::MIN, u16::MAX);
24 test_int!(0u32, u32::MIN, u32::MAX);
25 test_int!(0u64, u64::MIN, u64::MAX);
26 test_int!(0u128, u128::MIN, u128::MAX);
27
28 test_int!(0i8, i8::MIN, i8::MAX);
29 test_int!(0i16, i16::MIN, i16::MAX);
30 test_int!(0i32, i32::MIN, i32::MAX);
31 test_int!(0i64, i64::MIN, i64::MAX);
32 test_int!(0i128, i128::MIN, i128::MAX);
33
34 m!('a', '\u{0}'..=char::MAX);
35 m!('a', '\u{0}'..='\u{10_FFFF}');
36 // We can get away with just covering the following two ranges, which correspond to all valid
37 // Unicode Scalar Values.
38 m!('a', '\u{0}'..='\u{D7FF}' | '\u{E000}'..=char::MAX);
39 m!('a', '\u{0}'..'\u{D7FF}' | '\u{D7FF}' | '\u{E000}'..=char::MAX);
40
41 let 0..=255 = 0u8;
42 let -128..=127 = 0i8;
43 let -2147483648..=2147483647 = 0i32;
44 let '\u{0000}'..='\u{10FFFF}' = 'v';
45
46 // Almost exhaustive
47 m!(0u8, 0..255); //~ ERROR non-exhaustive patterns
48 m!(0u8, 0..=254); //~ ERROR non-exhaustive patterns
49 m!(0u8, 1..=255); //~ ERROR non-exhaustive patterns
50 m!(0u8, 0..42 | 43..=255); //~ ERROR non-exhaustive patterns
51 m!(0i8, -128..127); //~ ERROR non-exhaustive patterns
52 m!(0i8, -128..=126); //~ ERROR non-exhaustive patterns
53 m!(0i8, -127..=127); //~ ERROR non-exhaustive patterns
54 match 0i8 { //~ ERROR non-exhaustive patterns
55 i8::MIN ..= -1 => {}
56 1 ..= i8::MAX => {}
57 }
58 const ALMOST_MAX: u128 = u128::MAX - 1;
59 m!(0u128, 0..=ALMOST_MAX); //~ ERROR non-exhaustive patterns
60 m!(0u128, 0..=4); //~ ERROR non-exhaustive patterns
61 m!(0u128, 1..=u128::MAX); //~ ERROR non-exhaustive patterns
62
63 // More complicatedly (non-)exhaustive
64 match 0u8 {
65 0 ..= 30 => {}
66 20 ..= 70 => {}
67 50 ..= 255 => {}
68 }
69 match (0u8, true) { //~ ERROR non-exhaustive patterns
70 (0 ..= 125, false) => {}
71 (128 ..= 255, false) => {}
72 (0 ..= 255, true) => {}
73 }
74 match (0u8, true) { // ok
75 (0 ..= 125, false) => {}
76 (128 ..= 255, false) => {}
77 (0 ..= 255, true) => {}
78 (125 .. 128, false) => {}
79 }
80 match (true, 0u8) {
81 (true, 0 ..= 255) => {}
82 (false, 0 ..= 125) => {}
83 (false, 128 ..= 255) => {}
84 (false, 125 .. 128) => {}
85 }
86 match Some(0u8) {
87 None => {}
88 Some(0 ..= 125) => {}
89 Some(128 ..= 255) => {}
90 Some(125 .. 128) => {}
91 }
92 const FOO: u8 = 41;
93 const BAR: &u8 = &42;
94 match &0u8 {
95 0..41 => {}
96 &FOO => {}
97 BAR => {}
98 43..=255 => {}
99 }
100
101}