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.
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.
11 #![feature(exhaustive_integer_patterns)]
12 #![feature(exclusive_range_pattern)]
13 #![deny(unreachable_patterns)]
15 use std::{char, usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128};
20 // A single range covering the entire domain.
25 // A combination of ranges and values.
26 // These are currently allowed to be overlapping.
32 200 => {} //~ ERROR unreachable pattern
36 // An incomplete set of values.
37 match x { //~ ERROR non-exhaustive patterns
41 // A more incomplete set of values.
42 match x { //~ ERROR non-exhaustive patterns
50 match x { //~ ERROR non-exhaustive patterns
53 -2..=20 => {} //~ ERROR unreachable pattern
57 // Let's test other types too!
58 let c: char = '\u{0}';
60 '\u{0}' ..= char::MAX => {} // ok
63 // We can actually get away with just covering the
64 // following two ranges, which correspond to all
65 // valid Unicode Scalar Values.
67 '\u{0000}' ..= '\u{D7FF}' => {}
68 '\u{E000}' ..= '\u{10_FFFF}' => {}
72 0 ..= usize::MAX => {} // ok
76 0 ..= u16::MAX => {} // ok
80 0 ..= u32::MAX => {} // ok
84 0 ..= u64::MAX => {} // ok
88 0 ..= u128::MAX => {} // ok
92 isize::MIN ..= isize::MAX => {} // ok
96 -128 ..= 127 => {} // ok
99 match 0i8 { //~ ERROR non-exhaustive patterns
104 i16::MIN ..= i16::MAX => {} // ok
107 match 0i16 { //~ ERROR non-exhaustive patterns
108 i16::MIN ..= -1 => {}
113 i32::MIN ..= i32::MAX => {} // ok
117 i64::MIN ..= i64::MAX => {} // ok
121 i128::MIN ..= i128::MAX => {} // ok
124 // Make sure that guards don't factor into the exhaustiveness checks.
125 match 0u8 { //~ ERROR non-exhaustive patterns
127 128 ..= 255 if true => {}
132 128 ..= 255 if false => {}
133 128 ..= 255 => {} // ok, because previous arm was guarded
136 // Now things start getting a bit more interesting. Testing products!
137 match (0u8, Some(())) { //~ ERROR non-exhaustive patterns
142 match (0u8, true) { //~ ERROR non-exhaustive patterns
143 (0 ..= 125, false) => {}
144 (128 ..= 255, false) => {}
145 (0 ..= 255, true) => {}
148 match (0u8, true) { // ok
149 (0 ..= 125, false) => {}
150 (128 ..= 255, false) => {}
151 (0 ..= 255, true) => {}
152 (125 .. 128, false) => {}
161 const LIM: u128 = u128::MAX - 1;
162 match 0u128 { //~ ERROR non-exhaustive patterns
166 match 0u128 { //~ ERROR non-exhaustive patterns
170 match 0u128 { //~ ERROR non-exhaustive patterns
171 4 ..= u128::MAX => {}