Skip to content

Commit

Permalink
Fix irrefutable slice patterns in const fn
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 9, 2019
1 parent aef6288 commit 80262e6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -470,6 +470,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
}
}

ProjectionElem::ConstantIndex {..} |
ProjectionElem::Subslice {..} |
ProjectionElem::Field(..) |
ProjectionElem::Index(_) => {
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
Expand Down Expand Up @@ -499,8 +501,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
this.qualif.restrict(ty, this.tcx, this.param_env);
}

ProjectionElem::ConstantIndex {..} |
ProjectionElem::Subslice {..} |
ProjectionElem::Downcast(..) => {
this.not_const()
}
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_mir/transform/qualify_min_const_fn.rs
Expand Up @@ -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()))
}
Expand Down
11 changes: 11 additions & 0 deletions 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
}
5 changes: 5 additions & 0 deletions 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
}
9 changes: 9 additions & 0 deletions 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`.

0 comments on commit 80262e6

Please sign in to comment.