Skip to content

Commit 80262e6

Browse files
committed
Fix irrefutable slice patterns in const fn
1 parent aef6288 commit 80262e6

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
470470
}
471471
}
472472

473+
ProjectionElem::ConstantIndex {..} |
474+
ProjectionElem::Subslice {..} |
473475
ProjectionElem::Field(..) |
474476
ProjectionElem::Index(_) => {
475477
let base_ty = proj.base.ty(this.mir, this.tcx).to_ty(this.tcx);
@@ -499,8 +501,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
499501
this.qualif.restrict(ty, this.tcx, this.param_env);
500502
}
501503

502-
ProjectionElem::ConstantIndex {..} |
503-
ProjectionElem::Subslice {..} |
504504
ProjectionElem::Downcast(..) => {
505505
this.not_const()
506506
}

src/librustc_mir/transform/qualify_min_const_fn.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,10 @@ fn check_place(
264264
Place::Static(_) => Err((span, "cannot access `static` items in const fn".into())),
265265
Place::Projection(proj) => {
266266
match proj.elem {
267+
| ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. }
267268
| ProjectionElem::Deref | ProjectionElem::Field(..) | ProjectionElem::Index(_) => {
268269
check_place(tcx, mir, &proj.base, span)
269270
}
270-
// slice patterns are unstable
271-
| ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
272-
return Err((span, "slice patterns in const fn are unstable".into()))
273-
}
274271
| ProjectionElem::Downcast(..) => {
275272
Err((span, "`match` or `if let` in `const fn` is unstable".into()))
276273
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-pass
2+
3+
fn main() {}
4+
5+
const fn tup((a, b): (i32, i32)) -> i32 {
6+
a + b
7+
}
8+
9+
const fn array([a, b]: [i32; 2]) -> i32 {
10+
a + b
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {}
2+
3+
const fn slice([a, b]: &[i32]) -> i32 { //~ ERROR refutable pattern in function argument
4+
a + b
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0005]: refutable pattern in function argument: `&[]` not covered
2+
--> $DIR/const_let_refutable.rs:3:16
3+
|
4+
LL | const fn slice([a, b]: &[i32]) -> i32 { //~ ERROR refutable pattern in function argument
5+
| ^^^^^^ pattern `&[]` not covered
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0005`.

0 commit comments

Comments
 (0)