1
Fork 0

Fix bug in matching on floating-point ranges

This commit is contained in:
varkor 2018-11-30 21:13:16 +00:00
parent 0fb52fb538
commit 4406391cdc
8 changed files with 35 additions and 56 deletions

View file

@ -812,8 +812,17 @@ impl<'tcx> IntRange<'tcx> {
fn from_ctor(tcx: TyCtxt<'_, 'tcx, 'tcx>,
ctor: &Constructor<'tcx>)
-> Option<IntRange<'tcx>> {
// Floating-point ranges are permitted and we don't want
// to consider them when constructing integer ranges.
fn is_integral<'tcx>(ty: Ty<'tcx>) -> bool {
match ty.sty {
ty::Char | ty::Int(_) | ty::Uint(_) => true,
_ => false,
}
}
match ctor {
ConstantRange(lo, hi, ty, end) => {
ConstantRange(lo, hi, ty, end) if is_integral(ty) => {
// Perform a shift if the underlying types are signed,
// which makes the interval arithmetic simpler.
let bias = IntRange::signed_bias(tcx, ty);
@ -826,7 +835,7 @@ impl<'tcx> IntRange<'tcx> {
Some(IntRange { range: lo..=(hi - offset), ty })
}
}
ConstantValue(val) => {
ConstantValue(val) if is_integral(val.ty) => {
let ty = val.ty;
if let Some(val) = val.assert_bits(tcx, ty::ParamEnv::empty().and(ty)) {
let bias = IntRange::signed_bias(tcx, ty);
@ -836,9 +845,7 @@ impl<'tcx> IntRange<'tcx> {
None
}
}
Single | Variant(_) | Slice(_) => {
None
}
_ => None,
}
}

View file

@ -1,8 +0,0 @@
warning: unreachable pattern
--> $DIR/vec-matching-autoslice.rs:31:9
|
LL | ([_, _], _) => panic!(),
| ^^^^^^^^^^^
|
= note: #[warn(unreachable_patterns)] on by default

View file

@ -1,20 +0,0 @@
warning: unreachable pattern
--> $DIR/match-range.rs:47:7
|
LL | _ => panic!("should match float range")
| ^
|
= note: #[warn(unreachable_patterns)] on by default
warning: unreachable pattern
--> $DIR/match-range.rs:55:9
|
LL | _ => {},
| ^
warning: unreachable pattern
--> $DIR/match-range.rs:59:9
|
LL | _ => panic!("should match the range start"),
| ^

View file

@ -1,8 +0,0 @@
warning: unreachable pattern
--> $DIR/issue-15881-model-lexer-dotdotdot.rs:41:7
|
LL | _ => panic!("should match float range")
| ^
|
= note: #[warn(unreachable_patterns)] on by default

View file

@ -1,8 +0,0 @@
warning: unreachable pattern
--> $DIR/issue-7222.rs:20:9
|
LL | _ => ()
| ^
|
= note: #[warn(unreachable_patterns)] on by default

View file

@ -62,11 +62,5 @@ error: unreachable pattern
LL | 0.02f64 => {}
| ^^^^^^^
error: unreachable pattern
--> $DIR/match-range-fail-dominate.rs:47:7
|
LL | _ => {}
| ^
error: aborting due to 6 previous errors
error: aborting due to 5 previous errors

View file

@ -0,0 +1,13 @@
#![allow(illegal_floating_point_literal_pattern)]
#![deny(unreachable_patterns)]
fn main() {
match 0.0 {
0.0..=1.0 => {}
_ => {} // ok
}
match 0.0 { //~ ERROR non-exhaustive patterns
0.0..=1.0 => {}
}
}

View file

@ -0,0 +1,9 @@
error[E0004]: non-exhaustive patterns: `_` not covered
--> $DIR/non-exhaustive-float-range-match.rs:10:11
|
LL | match 0.0 { //~ ERROR non-exhaustive patterns
| ^^^ pattern `_` not covered
error: aborting due to previous error
For more information about this error, try `rustc --explain E0004`.