Skip to content

Commit a8e0511

Browse files
authored
Rollup merge of #71688 - ecstatic-morse:const-downcast, r=oli-obk
Allow `Downcast` projections unconditionally in const-checking `ProjectionElem::Downcast` sounds scary, but it's really just the projection we use to access a particular enum variant. They usually appear in the lowering of a `match` statement, so they have been associated with control flow in const-checking, but they don't do any control flow by themselves. We already have a HIR pass that looks for `if` and `match` (even ones that have 1 or fewer reachable branches). That pass is double-checked by a MIR pass that looks for `SwitchInt`s and `FakeRead`s for match scrutinees. In my opinion, there's no need to look for `Downcast` as well. r? @oli-obk
2 parents 8192cb6 + 0592976 commit a8e0511

File tree

9 files changed

+6
-47
lines changed

9 files changed

+6
-47
lines changed

src/librustc_mir/transform/check_consts/ops.rs

-9
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ pub trait NonConstOp: std::fmt::Debug {
5353
}
5454
}
5555

56-
/// A `Downcast` projection.
57-
#[derive(Debug)]
58-
pub struct Downcast;
59-
impl NonConstOp for Downcast {
60-
fn feature_gate() -> Option<Symbol> {
61-
Some(sym::const_if_match)
62-
}
63-
}
64-
6556
/// A function call where the callee is a pointer.
6657
#[derive(Debug)]
6758
pub struct FnCallIndirect;

src/librustc_mir/transform/check_consts/validation.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
472472
}
473473

474474
ProjectionElem::ConstantIndex { .. }
475+
| ProjectionElem::Downcast(..)
475476
| ProjectionElem::Subslice { .. }
476477
| ProjectionElem::Field(..)
477478
| ProjectionElem::Index(_) => {
@@ -484,10 +485,6 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
484485
_ => {}
485486
}
486487
}
487-
488-
ProjectionElem::Downcast(..) => {
489-
self.check_op(ops::Downcast);
490-
}
491488
}
492489
}
493490

src/librustc_mir/transform/qualify_min_const_fn.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,6 @@ fn check_place(
286286
while let &[ref proj_base @ .., elem] = cursor {
287287
cursor = proj_base;
288288
match elem {
289-
ProjectionElem::Downcast(..) if !feature_allowed(tcx, def_id, sym::const_if_match) => {
290-
return Err((span, "`match` or `if let` in `const fn` is unstable".into()));
291-
}
292-
ProjectionElem::Downcast(_symbol, _variant_index) => {}
293-
294289
ProjectionElem::Field(..) => {
295290
let base_ty = Place::ty_from(place.local, &proj_base, body, tcx).ty;
296291
if let Some(def) = base_ty.ty_adt_def() {
@@ -303,6 +298,7 @@ fn check_place(
303298
}
304299
}
305300
ProjectionElem::ConstantIndex { .. }
301+
| ProjectionElem::Downcast(..)
306302
| ProjectionElem::Subslice { .. }
307303
| ProjectionElem::Deref
308304
| ProjectionElem::Index(_) => {}

src/test/compile-fail/consts/const-fn-error.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const fn f(x: usize) -> usize {
1010
//~| ERROR E0658
1111
//~| ERROR E0080
1212
//~| ERROR E0744
13-
//~| ERROR E0019
1413
sum += i;
1514
}
1615
sum

src/test/compile-fail/issue-52443.rs

-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ fn main() {
1111
//~| ERROR calls in constants are limited to constant functions
1212
//~| ERROR references in constants may only refer to immutable values
1313
//~| ERROR calls in constants are limited to constant functions
14-
//~| ERROR constant contains unimplemented expression type
1514
//~| ERROR evaluation of constant value failed
1615
}

src/test/ui/consts/control-flow/feature-gate-const-if-match.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,5 @@ fn main() { //[if_match]~ ERROR fatal error triggered by #[rustc_error]
113113
//[stock]~^ ERROR `match` is not allowed in a `const`
114114
if let Some(x) = Some(x) { x } else { 1 }
115115
//[stock]~^ ERROR `if` is not allowed in a `const`
116-
//[stock]~| ERROR constant contains unimplemented expression type
117116
}];
118117
}

src/test/ui/consts/control-flow/feature-gate-const-if-match.stock.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,6 @@ LL | const MATCH: i32 = match 0 { 1 => 2, _ => 0 };
237237
= note: see issue #49146 <https://github.com/rust-lang/rust/issues/49146> for more information
238238
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
239239

240-
error[E0019]: constant contains unimplemented expression type
241-
--> $DIR/feature-gate-const-if-match.rs:114:21
242-
|
243-
LL | if let Some(x) = Some(x) { x } else { 1 }
244-
| ^
245-
246-
error: aborting due to 25 previous errors
240+
error: aborting due to 24 previous errors
247241

248-
Some errors have detailed explanations: E0019, E0658.
249-
For more information about an error, try `rustc --explain E0019`.
242+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/or-patterns/feature-gate-const-fn.rs

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ fn main() {
3030
let x = Ok(3);
3131
let Ok(y) | Err(y) = x;
3232
//~^ ERROR or-pattern is not allowed in a `const`
33-
//~| ERROR constant contains unimplemented expression type
34-
//~| ERROR constant contains unimplemented expression type
3533
2
3634
}];
3735
}

src/test/ui/or-patterns/feature-gate-const-fn.stderr

+2-15
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,6 @@ LL | let Ok(y) | Err(y) = x;
5252
= note: see issue #49146 <https://github.com/rust-lang/rust/issues/49146> for more information
5353
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
5454

55-
error[E0019]: constant contains unimplemented expression type
56-
--> $DIR/feature-gate-const-fn.rs:31:25
57-
|
58-
LL | let Ok(y) | Err(y) = x;
59-
| ^
60-
61-
error[E0019]: constant contains unimplemented expression type
62-
--> $DIR/feature-gate-const-fn.rs:31:16
63-
|
64-
LL | let Ok(y) | Err(y) = x;
65-
| ^
66-
67-
error: aborting due to 8 previous errors
55+
error: aborting due to 6 previous errors
6856

69-
Some errors have detailed explanations: E0019, E0658.
70-
For more information about an error, try `rustc --explain E0019`.
57+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)