]> git.lizzy.rs Git - rust.git/commitdiff
Fix irrefutable slice patterns in const fn
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 9 Jan 2019 10:32:56 +0000 (11:32 +0100)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Wed, 9 Jan 2019 10:32:56 +0000 (11:32 +0100)
src/librustc_mir/transform/qualify_consts.rs
src/librustc_mir/transform/qualify_min_const_fn.rs
src/test/ui/consts/const_let_irrefutable.rs [new file with mode: 0644]
src/test/ui/consts/const_let_refutable.rs [new file with mode: 0644]
src/test/ui/consts/const_let_refutable.stderr [new file with mode: 0644]

index 3d90e55f1e4e257dd73eb6405692a6c3d7e11913..ddd714fd27e597a5b1f3bbe114def3edd74ae7ba 100644 (file)
@@ -470,6 +470,8 @@ fn visit_place(&mut self,
                             }
                         }
 
+                        ProjectionElem::ConstantIndex {..} |
+                        ProjectionElem::Subslice {..} |
                         ProjectionElem::Field(..) |
                         ProjectionElem::Index(_) => {
                             let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
@@ -499,8 +501,6 @@ fn visit_place(&mut self,
                             this.qualif.restrict(ty, this.tcx, this.param_env);
                         }
 
-                        ProjectionElem::ConstantIndex {..} |
-                        ProjectionElem::Subslice {..} |
                         ProjectionElem::Downcast(..) => {
                             this.not_const()
                         }
index 6df6841f869f28e97d955ab167a45a3fac2c6d5f..6935255098b6790bf68ec54985bc89e2d3edddd8 100644 (file)
@@ -264,13 +264,10 @@ fn check_place(
         Place::Static(_) => Err((span, "cannot access `static` items in const fn".into())),
         Place::Projection(proj) => {
             match proj.elem {
+                | ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. }
                 | ProjectionElem::Deref | ProjectionElem::Field(..) | ProjectionElem::Index(_) => {
                     check_place(tcx, mir, &proj.base, span)
                 }
-                // slice patterns are unstable
-                | ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
-                    return Err((span, "slice patterns in const fn are unstable".into()))
-                }
                 | ProjectionElem::Downcast(..) => {
                     Err((span, "`match` or `if let` in `const fn` is unstable".into()))
                 }
diff --git a/src/test/ui/consts/const_let_irrefutable.rs b/src/test/ui/consts/const_let_irrefutable.rs
new file mode 100644 (file)
index 0000000..424a16f
--- /dev/null
@@ -0,0 +1,11 @@
+// compile-pass
+
+fn main() {}
+
+const fn tup((a, b): (i32, i32)) -> i32 {
+    a + b
+}
+
+const fn array([a, b]: [i32; 2]) -> i32 {
+    a + b
+}
diff --git a/src/test/ui/consts/const_let_refutable.rs b/src/test/ui/consts/const_let_refutable.rs
new file mode 100644 (file)
index 0000000..345f682
--- /dev/null
@@ -0,0 +1,5 @@
+fn main() {}
+
+const fn slice([a, b]: &[i32]) -> i32 { //~ ERROR refutable pattern in function argument
+    a + b
+}
diff --git a/src/test/ui/consts/const_let_refutable.stderr b/src/test/ui/consts/const_let_refutable.stderr
new file mode 100644 (file)
index 0000000..c5d2ba0
--- /dev/null
@@ -0,0 +1,9 @@
+error[E0005]: refutable pattern in function argument: `&[]` not covered
+  --> $DIR/const_let_refutable.rs:3:16
+   |
+LL | const fn slice([a, b]: &[i32]) -> i32 { //~ ERROR refutable pattern in function argument
+   |                ^^^^^^ pattern `&[]` not covered
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0005`.