Skip to content

Commit 7476ba4

Browse files
committed
Add semi-exhaustive tests for exhaustiveness
1 parent 121fa8d commit 7476ba4

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: unreachable pattern
2+
--> $DIR/exhaustive_integer_patterns.rs:32:9
3+
|
4+
LL | 200 => {} //~ ERROR unreachable pattern
5+
| ^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/exhaustive_integer_patterns.rs:13:9
9+
|
10+
LL | #![deny(unreachable_patterns)]
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0004]: non-exhaustive patterns: `128u8...255u8` not covered
14+
--> $DIR/exhaustive_integer_patterns.rs:37:11
15+
|
16+
LL | match x { //~ ERROR non-exhaustive patterns: `128u8...255u8` not covered
17+
| ^ pattern `128u8...255u8` not covered
18+
19+
error[E0004]: non-exhaustive patterns: `11u8...20u8`, `30u8...35u8`, `35u8...70u8` and 1 more not covered
20+
--> $DIR/exhaustive_integer_patterns.rs:42:11
21+
|
22+
LL | match x { //~ ERROR non-exhaustive patterns
23+
| ^ patterns `11u8...20u8`, `30u8...35u8`, `35u8...70u8` and 1 more not covered
24+
25+
error: unreachable pattern
26+
--> $DIR/exhaustive_integer_patterns.rs:53:9
27+
|
28+
LL | -2..=20 => {} //~ ERROR unreachable pattern
29+
| ^^^^^^^
30+
31+
error[E0004]: non-exhaustive patterns: `-128i8...-5i8`, `120i8...121i8` and `121i8...127i8` not covered
32+
--> $DIR/exhaustive_integer_patterns.rs:50:11
33+
|
34+
LL | match x { //~ ERROR non-exhaustive patterns
35+
| ^ patterns `-128i8...-5i8`, `120i8...121i8` and `121i8...127i8` not covered
36+
37+
error: aborting due to 5 previous errors
38+
39+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)