Skip to content

Commit 365d123

Browse files
Add feature gate for const if and match
1 parent c537f22 commit 365d123

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

src/librustc_mir/transform/check_consts/ops.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ impl NonConstOp for HeapAllocation {
139139
#[derive(Debug)]
140140
pub struct IfOrMatch;
141141
impl NonConstOp for IfOrMatch {
142+
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
143+
Some(tcx.features().const_if_match)
144+
}
145+
142146
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
143147
// This should be caught by the HIR const-checker.
144148
item.tcx.sess.delay_span_bug(

src/librustc_mir/transform/qualify_min_const_fn.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ fn check_statement(
216216
check_rvalue(tcx, body, def_id, rval, span)
217217
}
218218

219-
StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {
219+
| StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _)
220+
if !tcx.features().const_if_match
221+
=> {
220222
Err((span, "loops and conditional expressions are not stable in const fn".into()))
221223
}
222224

@@ -324,10 +326,17 @@ fn check_terminator(
324326
check_operand(tcx, value, span, def_id, body)
325327
},
326328

327-
TerminatorKind::FalseEdges { .. } | TerminatorKind::SwitchInt { .. } => Err((
329+
| TerminatorKind::FalseEdges { .. }
330+
| TerminatorKind::SwitchInt { .. }
331+
if !tcx.features().const_if_match
332+
=> Err((
328333
span,
329334
"loops and conditional expressions are not stable in const fn".into(),
330335
)),
336+
| TerminatorKind::FalseEdges { .. }
337+
| TerminatorKind::SwitchInt { .. }
338+
=> Ok(()),
339+
331340
| TerminatorKind::Abort | TerminatorKind::Unreachable => {
332341
Err((span, "const fn with unreachable code is not stable".into()))
333342
}

src/librustc_passes/check_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
135135
self.const_check_violated(source.name(), e.span);
136136
}
137137

138-
hir::ExprKind::Match(_, _, source) => {
138+
hir::ExprKind::Match(_, _, source) if !self.tcx.features().const_if_match => {
139139
use hir::MatchSource::*;
140140

141141
let op = match source {

src/libsyntax/feature_gate/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ declare_features! (
529529
/// Allows using the `#[register_attr]` attribute.
530530
(active, register_tool, "1.41.0", Some(66079), None),
531531

532+
/// Allows the use of `if` and `match` in constants.
533+
(active, const_if_match, "1.41.0", Some(49146), None),
534+
532535
// -------------------------------------------------------------------------
533536
// feature-group-end: actual feature gates
534537
// -------------------------------------------------------------------------

src/libsyntax_pos/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ symbols! {
202202
const_fn,
203203
const_fn_union,
204204
const_generics,
205+
const_if_match,
205206
const_indexing,
206207
const_in_array_repeat_expressions,
207208
const_let,

0 commit comments

Comments
 (0)