about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2019-11-17 22:43:06 +0000
committerNadrieril <nadrieril+git@gmail.com>2019-11-21 11:20:46 +0000
commit5510f5589eac9504f7fdbd5673ff64e2bddd88ab (patch)
tree52c8cbf097cba33fb5006744b3c2ea9e54982163 /src
parentfa4a4d3edaf48e9a08f6357765eef8714b424cdf (diff)
downloadrust-5510f5589eac9504f7fdbd5673ff64e2bddd88ab.tar.gz
rust-5510f5589eac9504f7fdbd5673ff64e2bddd88ab.zip
Use appropriate constructor for const slices
Diffstat (limited to 'src')
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs16
-rw-r--r--src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs4
-rw-r--r--src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.stderr14
-rw-r--r--src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr8
-rw-r--r--src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs2
-rw-r--r--src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr4
6 files changed, 23 insertions, 25 deletions
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index 5e7a7f01e7a..5fcaeca28a0 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -747,7 +747,7 @@ impl<'tcx> Constructor<'tcx> {
                     .iter()
                     .filter_map(|c: &Constructor<'_>| match c {
                         Slice(slice) => Some(*slice),
-                        // FIXME(#65413): We ignore `ConstantValue`s here.
+                        // FIXME(oli-obk): implement `deref` for `ConstValue`
                         ConstantValue(..) => None,
                         _ => bug!("bad slice pattern constructor {:?}", c),
                     })
@@ -1771,7 +1771,19 @@ fn pat_constructor<'tcx>(
             if let Some(int_range) = IntRange::from_const(tcx, param_env, value, pat.span) {
                 Some(IntRange(int_range))
             } else {
-                Some(ConstantValue(value))
+                match (value.val, &value.ty.kind) {
+                    (_, ty::Array(_, n)) => {
+                        let len = n.eval_usize(tcx, param_env);
+                        Some(Slice(Slice { array_len: Some(len), kind: FixedLen(len) }))
+                    }
+                    (ty::ConstKind::Value(ConstValue::Slice { start, end, .. }), ty::Slice(_)) => {
+                        let len = (end - start) as u64;
+                        Some(Slice(Slice { array_len: None, kind: FixedLen(len) }))
+                    }
+                    // FIXME(oli-obk): implement `deref` for `ConstValue`
+                    // (ty::ConstKind::Value(ConstValue::ByRef { .. }), ty::Slice(_)) => { ... }
+                    _ => Some(ConstantValue(value)),
+                }
             }
         }
         PatKind::Range(PatRange { lo, hi, end }) => {
diff --git a/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs b/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs
index cca4173e3ef..6c54c938bf1 100644
--- a/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs
+++ b/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.rs
@@ -1,3 +1,4 @@
+// check-pass
 #![feature(slice_patterns)]
 #![deny(unreachable_patterns)]
 
@@ -8,8 +9,7 @@ fn main() {
     match x {
         &[] => {}
         &[1..=255] => {}
-        // this shouldn't be unreachable
-        C0 => {} //~ unreachable pattern
+        C0 => {}
         &[_, _, ..] => {}
     }
 }
diff --git a/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.stderr b/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.stderr
deleted file mode 100644
index 74421b4085c..00000000000
--- a/src/test/ui/pattern/usefulness/65413-constants-and-slices-exhaustiveness.stderr
+++ /dev/null
@@ -1,14 +0,0 @@
-error: unreachable pattern
-  --> $DIR/65413-constants-and-slices-exhaustiveness.rs:12:9
-   |
-LL |         C0 => {}
-   |         ^^
-   |
-note: lint level defined here
-  --> $DIR/65413-constants-and-slices-exhaustiveness.rs:2:9
-   |
-LL | #![deny(unreachable_patterns)]
-   |         ^^^^^^^^^^^^^^^^^^^^
-
-error: aborting due to previous error
-
diff --git a/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr b/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr
index 63ed49094fc..539aa854f9e 100644
--- a/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr
+++ b/src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr
@@ -1,16 +1,16 @@
-error[E0004]: non-exhaustive patterns: `&[..]` not covered
+error[E0004]: non-exhaustive patterns: `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
   --> $DIR/match-byte-array-patterns-2.rs:4:11
    |
 LL |     match buf {
-   |           ^^^ pattern `&[..]` not covered
+   |           ^^^ patterns `&[0u8..=64u8, _, _, _]` and `&[66u8..=std::u8::MAX, _, _, _]` not covered
    |
    = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
 
-error[E0004]: non-exhaustive patterns: `&[..]` not covered
+error[E0004]: non-exhaustive patterns: `&[]`, `&[_]`, `&[_, _]` and 2 more not covered
   --> $DIR/match-byte-array-patterns-2.rs:10:11
    |
 LL |     match buf {
-   |           ^^^ pattern `&[..]` not covered
+   |           ^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 2 more not covered
    |
    = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
 
diff --git a/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs b/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs
index 88a47fc074f..41ba2cc4501 100644
--- a/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs
+++ b/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.rs
@@ -97,7 +97,7 @@ fn main() {
     }
     const CONST1: &[bool; 1] = &[true];
     match s1 {
-    //~^ ERROR `&[..]` not covered
+    //~^ ERROR `&[false]` not covered
         CONST1 => {}
     }
     match s1 {
diff --git a/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr b/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr
index 2295de02ed5..8cb342f33df 100644
--- a/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr
+++ b/src/test/ui/pattern/usefulness/slice-patterns-exhaustiveness.stderr
@@ -118,11 +118,11 @@ LL |     match s {
    |
    = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
 
-error[E0004]: non-exhaustive patterns: `&[..]` not covered
+error[E0004]: non-exhaustive patterns: `&[false]` not covered
   --> $DIR/slice-patterns-exhaustiveness.rs:99:11
    |
 LL |     match s1 {
-   |           ^^ pattern `&[..]` not covered
+   |           ^^ pattern `&[false]` not covered
    |
    = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms