]> git.lizzy.rs Git - rust.git/commitdiff
Add feature gate for const `if` and `match`
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Wed, 18 Sep 2019 17:27:31 +0000 (10:27 -0700)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Thu, 21 Nov 2019 22:19:59 +0000 (14:19 -0800)
src/librustc_mir/transform/check_consts/ops.rs
src/librustc_mir/transform/qualify_min_const_fn.rs
src/librustc_passes/check_const.rs
src/libsyntax/feature_gate/active.rs
src/libsyntax_pos/symbol.rs

index 80f2925193a8140a87785e1b8b0a273d6b0ab347..950f48f03cd97068dea11fa18c3c949aeebf0b00 100644 (file)
@@ -139,6 +139,10 @@ fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
 #[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(
index 83bde5ed34eaeca6f080407088b17b28bd70de8d..cb6f94adbf08cb7a5154eec490f260d5bc25069d 100644 (file)
@@ -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()))
         }
index 9d37b4bdb769c89216619afa39844e19abf1e6c6..9870a78bd447205db50123599a97fa4b210344bf 100644 (file)
@@ -135,7 +135,7 @@ fn visit_expr(&mut self, e: &'tcx hir::Expr) {
                 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 {
index bd029514a95245aef7d522e44cab8fba329cb7a0..fa0ab90c702b9621b5c50c640ccc5ff3b7742bbf 100644 (file)
@@ -529,6 +529,9 @@ pub fn set(&self, features: &mut Features, span: Span) {
     /// 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
     // -------------------------------------------------------------------------
index 979074f17c7e0561be76bd76560663a27d917dfe..e7238caca8e4b685277932d2d62730767fc429f9 100644 (file)
         const_fn,
         const_fn_union,
         const_generics,
+        const_if_match,
         const_indexing,
         const_in_array_repeat_expressions,
         const_let,