Skip to content

Commit

Permalink
Fix bug in matching on floating-point ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Nov 30, 2018
1 parent 0fb52fb commit 4406391
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 56 deletions.
17 changes: 12 additions & 5 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -836,9 +845,7 @@ impl<'tcx> IntRange<'tcx> {
None
}
}
Single | Variant(_) | Slice(_) => {
None
}
_ => None,
}
}

Expand Down

This file was deleted.

20 changes: 0 additions & 20 deletions src/test/run-pass/binding/match-range.stderr

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/run-pass/issues/issue-7222.stderr

This file was deleted.

8 changes: 1 addition & 7 deletions src/test/ui/match/match-range-fail-dominate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

13 changes: 13 additions & 0 deletions src/test/ui/non-exhaustive/non-exhaustive-float-range-match.rs
Original file line number Diff line number Diff line change
@@ -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 => {}
}
}
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 4406391

Please sign in to comment.