about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2018-11-30 21:13:16 +0000
committervarkor <github@varkor.com>2018-11-30 21:15:48 +0000
commit4406391cdce37dfd8142cc49e3760c606da8913c (patch)
tree6851b010c32c5863ded0d14afe928270e91e9bbc
parent0fb52fb538946600dfe76be0b943556363904136 (diff)
downloadrust-4406391cdce37dfd8142cc49e3760c606da8913c.tar.gz
rust-4406391cdce37dfd8142cc49e3760c606da8913c.zip
Fix bug in matching on floating-point ranges
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs17
-rw-r--r--src/test/run-pass/array-slice-vec/vec-matching-autoslice.stderr8
-rw-r--r--src/test/run-pass/binding/match-range.stderr20
-rw-r--r--src/test/run-pass/issues/issue-15881-model-lexer-dotdotdot.stderr8
-rw-r--r--src/test/run-pass/issues/issue-7222.stderr8
-rw-r--r--src/test/ui/match/match-range-fail-dominate.stderr8
-rw-r--r--src/test/ui/non-exhaustive/non-exhaustive-float-range-match.rs13
-rw-r--r--src/test/ui/non-exhaustive/non-exhaustive-float-range-match.stderr9
8 files changed, 35 insertions, 56 deletions
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index a2fbfd70a65..f7e034df1da 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -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,
         }
     }
 
diff --git a/src/test/run-pass/array-slice-vec/vec-matching-autoslice.stderr b/src/test/run-pass/array-slice-vec/vec-matching-autoslice.stderr
deleted file mode 100644
index fd811e8083c..00000000000
--- a/src/test/run-pass/array-slice-vec/vec-matching-autoslice.stderr
+++ /dev/null
@@ -1,8 +0,0 @@
-warning: unreachable pattern
-  --> $DIR/vec-matching-autoslice.rs:31:9
-   |
-LL |         ([_, _], _) => panic!(),
-   |         ^^^^^^^^^^^
-   |
-   = note: #[warn(unreachable_patterns)] on by default
-
diff --git a/src/test/run-pass/binding/match-range.stderr b/src/test/run-pass/binding/match-range.stderr
deleted file mode 100644
index f402a98af49..00000000000
--- a/src/test/run-pass/binding/match-range.stderr
+++ /dev/null
@@ -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"),
-   |         ^
-
diff --git a/src/test/run-pass/issues/issue-15881-model-lexer-dotdotdot.stderr b/src/test/run-pass/issues/issue-15881-model-lexer-dotdotdot.stderr
deleted file mode 100644
index 41b4130acf4..00000000000
--- a/src/test/run-pass/issues/issue-15881-model-lexer-dotdotdot.stderr
+++ /dev/null
@@ -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
-
diff --git a/src/test/run-pass/issues/issue-7222.stderr b/src/test/run-pass/issues/issue-7222.stderr
deleted file mode 100644
index 1b231f2da39..00000000000
--- a/src/test/run-pass/issues/issue-7222.stderr
+++ /dev/null
@@ -1,8 +0,0 @@
-warning: unreachable pattern
-  --> $DIR/issue-7222.rs:20:9
-   |
-LL |         _ => ()
-   |         ^
-   |
-   = note: #[warn(unreachable_patterns)] on by default
-
diff --git a/src/test/ui/match/match-range-fail-dominate.stderr b/src/test/ui/match/match-range-fail-dominate.stderr
index 99f65042d85..d75630e09c5 100644
--- a/src/test/ui/match/match-range-fail-dominate.stderr
+++ b/src/test/ui/match/match-range-fail-dominate.stderr
@@ -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
 
diff --git a/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.rs b/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.rs
new file mode 100644
index 00000000000..588fecbf10d
--- /dev/null
+++ b/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.rs
@@ -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 => {}
+    }
+}
diff --git a/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.stderr b/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.stderr
new file mode 100644
index 00000000000..2e285afb380
--- /dev/null
+++ b/src/test/ui/non-exhaustive/non-exhaustive-float-range-match.stderr
@@ -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`.