1 #![feature(precise_pointer_size_matching)]
2 #![feature(exclusive_range_pattern)]
3 #![deny(unreachable_patterns)]
4 #![deny(overlapping_patterns)]
6 use std
::{char, u8, u16, u32, u64, u128, i8, i16, i32, i64, i128}
;
11 // A single range covering the entire domain.
16 // A combination of ranges and values.
17 // These are currently allowed to be overlapping.
23 200 => {}
//~ ERROR unreachable pattern
27 // An incomplete set of values.
28 match x
{ //~ ERROR non-exhaustive patterns
32 // A more incomplete set of values.
33 match x
{ //~ ERROR non-exhaustive patterns
41 match x
{ //~ ERROR non-exhaustive patterns
45 //~^ ERROR unreachable pattern
49 // Let's test other types too!
50 let c
: char = '
\u{0}'
;
52 '
\u{0}'
..= char::MAX
=> {}
// ok
55 // We can actually get away with just covering the
56 // following two ranges, which correspond to all
57 // valid Unicode Scalar Values.
59 '
\u{0000}'
..= '
\u{D7FF}'
=> {}
60 '
\u{E000}'
..= '
\u{10_FFFF}'
=> {}
64 0 ..= u16::MAX
=> {}
// ok
68 0 ..= u32::MAX
=> {}
// ok
72 0 ..= u64::MAX
=> {}
// ok
76 0 ..= u128
::MAX
=> {}
// ok
80 -128 ..= 127 => {}
// ok
83 match 0i8 { //~ ERROR non-exhaustive patterns
88 i16::MIN
..= i16::MAX
=> {}
// ok
91 match 0i16 { //~ ERROR non-exhaustive patterns
97 i32::MIN
..= i32::MAX
=> {}
// ok
101 i64::MIN
..= i64::MAX
=> {}
// ok
105 i128
::MIN
..= i128
::MAX
=> {}
// ok
108 // Make sure that guards don't factor into the exhaustiveness checks.
109 match 0u8 { //~ ERROR non-exhaustive patterns
111 128 ..= 255 if true => {}
116 128 ..= 255 if false => {}
117 128 ..= 255 => {}
// ok, because previous arm was guarded
120 // Now things start getting a bit more interesting. Testing products!
121 match (0u8, Some(())) { //~ ERROR non-exhaustive patterns
126 match (0u8, true) { //~ ERROR non-exhaustive patterns
127 (0 ..= 125, false) => {}
128 (128 ..= 255, false) => {}
129 (0 ..= 255, true) => {}
132 match (0u8, true) { // ok
133 (0 ..= 125, false) => {}
134 (128 ..= 255, false) => {}
135 (0 ..= 255, true) => {}
136 (125 .. 128, false) => {}
141 1 ..= 2 => {}
//~ ERROR multiple patterns covering the same range
145 const LIM
: u128
= u128
::MAX
- 1;
146 match 0u128 { //~ ERROR non-exhaustive patterns
150 match 0u128 { //~ ERROR non-exhaustive patterns
154 match 0u128 { //~ ERROR non-exhaustive patterns
155 4 ..= u128
::MAX
=> {}
159 const BAR
: &i32 = &42;
162 &FOO
=> {}
//~ ERROR unreachable pattern
163 BAR
=> {}
// Not detected as unreachable because `try_eval_bits` fails on `BAR`.
167 // Regression test, see https://github.com/rust-lang/rust/pull/66326#issuecomment-552889933