Add feature gate for const if and match
This commit is contained in:
parent
c537f22900
commit
365d123689
5 changed files with 20 additions and 3 deletions
|
|
@ -139,6 +139,10 @@ impl NonConstOp for HeapAllocation {
|
|||
#[derive(Debug)]
|
||||
pub struct IfOrMatch;
|
||||
impl NonConstOp for IfOrMatch {
|
||||
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
|
||||
Some(tcx.features().const_if_match)
|
||||
}
|
||||
|
||||
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
|
||||
// This should be caught by the HIR const-checker.
|
||||
item.tcx.sess.delay_span_bug(
|
||||
|
|
|
|||
|
|
@ -216,7 +216,9 @@ fn check_statement(
|
|||
check_rvalue(tcx, body, def_id, rval, span)
|
||||
}
|
||||
|
||||
StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {
|
||||
| StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _)
|
||||
if !tcx.features().const_if_match
|
||||
=> {
|
||||
Err((span, "loops and conditional expressions are not stable in const fn".into()))
|
||||
}
|
||||
|
||||
|
|
@ -324,10 +326,17 @@ fn check_terminator(
|
|||
check_operand(tcx, value, span, def_id, body)
|
||||
},
|
||||
|
||||
TerminatorKind::FalseEdges { .. } | TerminatorKind::SwitchInt { .. } => Err((
|
||||
| TerminatorKind::FalseEdges { .. }
|
||||
| TerminatorKind::SwitchInt { .. }
|
||||
if !tcx.features().const_if_match
|
||||
=> Err((
|
||||
span,
|
||||
"loops and conditional expressions are not stable in const fn".into(),
|
||||
)),
|
||||
| TerminatorKind::FalseEdges { .. }
|
||||
| TerminatorKind::SwitchInt { .. }
|
||||
=> Ok(()),
|
||||
|
||||
| TerminatorKind::Abort | TerminatorKind::Unreachable => {
|
||||
Err((span, "const fn with unreachable code is not stable".into()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
|
|||
self.const_check_violated(source.name(), e.span);
|
||||
}
|
||||
|
||||
hir::ExprKind::Match(_, _, source) => {
|
||||
hir::ExprKind::Match(_, _, source) if !self.tcx.features().const_if_match => {
|
||||
use hir::MatchSource::*;
|
||||
|
||||
let op = match source {
|
||||
|
|
|
|||
|
|
@ -529,6 +529,9 @@ declare_features! (
|
|||
/// Allows using the `#[register_attr]` attribute.
|
||||
(active, register_tool, "1.41.0", Some(66079), None),
|
||||
|
||||
/// Allows the use of `if` and `match` in constants.
|
||||
(active, const_if_match, "1.41.0", Some(49146), None),
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// feature-group-end: actual feature gates
|
||||
// -------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -202,6 +202,7 @@ symbols! {
|
|||
const_fn,
|
||||
const_fn_union,
|
||||
const_generics,
|
||||
const_if_match,
|
||||
const_indexing,
|
||||
const_in_array_repeat_expressions,
|
||||
const_let,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue