From: Oliver Scherer Date: Wed, 9 Jan 2019 10:32:56 +0000 (+0100) Subject: Fix irrefutable slice patterns in const fn X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=80262e6040675b9346816e2845a7fc81c64b82a0;p=rust.git Fix irrefutable slice patterns in const fn --- diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 3d90e55f1e4..ddd714fd27e 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -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() } diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 6df6841f869..6935255098b 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -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 index 00000000000..424a16f7ed3 --- /dev/null +++ b/src/test/ui/consts/const_let_irrefutable.rs @@ -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 index 00000000000..345f682868f --- /dev/null +++ b/src/test/ui/consts/const_let_refutable.rs @@ -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 index 00000000000..c5d2ba02a70 --- /dev/null +++ b/src/test/ui/consts/const_let_refutable.stderr @@ -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`.