Skip to content

Commit 2471179

Browse files
committed
Fix bug I introduced and test float ranges better
1 parent 48442a1 commit 2471179

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,12 +1408,12 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
14081408
ty::Float(ty::FloatTy::F32) => {
14091409
let lo = rustc_apfloat::ieee::Single::from_bits(lo);
14101410
let hi = rustc_apfloat::ieee::Single::from_bits(hi);
1411-
F32Range(lo, hi, RangeEnd::Included)
1411+
F32Range(lo, hi, *end)
14121412
}
14131413
ty::Float(ty::FloatTy::F64) => {
14141414
let lo = rustc_apfloat::ieee::Double::from_bits(lo);
14151415
let hi = rustc_apfloat::ieee::Double::from_bits(hi);
1416-
F64Range(lo, hi, RangeEnd::Included)
1416+
F64Range(lo, hi, *end)
14171417
}
14181418
_ => bug!("invalid type for range pattern: {}", ty),
14191419
};

tests/ui/pattern/usefulness/floats.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
1+
#![feature(exclusive_range_pattern)]
12
#![allow(illegal_floating_point_literal_pattern)]
23
#![deny(unreachable_patterns)]
34

45
fn main() {
56
match 0.0 {
6-
0.0..=1.0 => {}
7-
_ => {} // ok
7+
0.0..=1.0 => {}
8+
_ => {} // ok
89
}
910

10-
match 0.0 { //~ ERROR non-exhaustive patterns
11-
0.0..=1.0 => {}
11+
match 0.0 {
12+
//~^ ERROR non-exhaustive patterns
13+
0.0..=1.0 => {}
1214
}
1315

1416
match 1.0f64 {
15-
0.01f64 ..= 6.5f64 => {}
16-
0.02f64 => {} //~ ERROR unreachable pattern
17-
_ => {}
17+
0.01f64..=6.5f64 => {}
18+
0.005f64 => {}
19+
0.01f64 => {} //~ ERROR unreachable pattern
20+
0.02f64 => {} //~ ERROR unreachable pattern
21+
6.5f64 => {} //~ ERROR unreachable pattern
22+
6.6f64 => {}
23+
1.0f64..=4.0f64 => {} //~ ERROR unreachable pattern
24+
5.0f64..=7.0f64 => {}
25+
_ => {}
26+
};
27+
match 1.0f64 {
28+
0.01f64..6.5f64 => {}
29+
6.5f64 => {} // this is reachable
30+
_ => {}
31+
};
32+
33+
match 1.0f32 {
34+
0.01f32..=6.5f32 => {}
35+
0.01f32 => {} //~ ERROR unreachable pattern
36+
0.02f32 => {} //~ ERROR unreachable pattern
37+
6.5f32 => {} //~ ERROR unreachable pattern
38+
_ => {}
39+
};
40+
match 1.0f32 {
41+
0.01f32..6.5f32 => {}
42+
6.5f32 => {} // this is reachable
43+
_ => {}
1844
};
1945
}
Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
11
error[E0004]: non-exhaustive patterns: `_` not covered
2-
--> $DIR/floats.rs:10:11
2+
--> $DIR/floats.rs:11:11
33
|
44
LL | match 0.0 {
55
| ^^^ pattern `_` not covered
66
|
77
= note: the matched value is of type `f64`
88
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
99
|
10-
LL ~ 0.0..=1.0 => {},
11-
LL + _ => todo!()
10+
LL ~ 0.0..=1.0 => {},
11+
LL + _ => todo!()
1212
|
1313

1414
error: unreachable pattern
15-
--> $DIR/floats.rs:16:7
15+
--> $DIR/floats.rs:19:9
1616
|
17-
LL | 0.02f64 => {}
18-
| ^^^^^^^
17+
LL | 0.01f64 => {}
18+
| ^^^^^^^
1919
|
2020
note: the lint level is defined here
21-
--> $DIR/floats.rs:2:9
21+
--> $DIR/floats.rs:3:9
2222
|
2323
LL | #![deny(unreachable_patterns)]
2424
| ^^^^^^^^^^^^^^^^^^^^
2525

26-
error: aborting due to 2 previous errors
26+
error: unreachable pattern
27+
--> $DIR/floats.rs:20:9
28+
|
29+
LL | 0.02f64 => {}
30+
| ^^^^^^^
31+
32+
error: unreachable pattern
33+
--> $DIR/floats.rs:21:9
34+
|
35+
LL | 6.5f64 => {}
36+
| ^^^^^^
37+
38+
error: unreachable pattern
39+
--> $DIR/floats.rs:23:9
40+
|
41+
LL | 1.0f64..=4.0f64 => {}
42+
| ^^^^^^^^^^^^^^^
43+
44+
error: unreachable pattern
45+
--> $DIR/floats.rs:35:9
46+
|
47+
LL | 0.01f32 => {}
48+
| ^^^^^^^
49+
50+
error: unreachable pattern
51+
--> $DIR/floats.rs:36:9
52+
|
53+
LL | 0.02f32 => {}
54+
| ^^^^^^^
55+
56+
error: unreachable pattern
57+
--> $DIR/floats.rs:37:9
58+
|
59+
LL | 6.5f32 => {}
60+
| ^^^^^^
61+
62+
error: aborting due to 8 previous errors
2763

2864
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)