]> git.lizzy.rs Git - rust.git/commitdiff
Add `const_fn_floating_point_arithmetic`
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Wed, 23 Sep 2020 18:53:58 +0000 (11:53 -0700)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Fri, 25 Sep 2020 17:37:52 +0000 (10:37 -0700)
compiler/rustc_feature/src/active.rs
compiler/rustc_mir/src/transform/check_consts/ops.rs
compiler/rustc_span/src/symbol.rs

index 6452bda293ef5465ff17d2895e04c9fd202a1d84..720fa9939f399bb4457ed2da50daaa4ef5e762b4 100644 (file)
@@ -584,6 +584,9 @@ pub fn set(&self, features: &mut Features, span: Span) {
     /// Allows non trivial generic constants which have to be manually propageted upwards.
     (active, const_evaluatable_checked, "1.48.0", Some(76560), None),
 
+    /// Allows basic arithmetic on floating point types in a `const fn`.
+    (active, const_fn_floating_point_arithmetic, "1.48.0", Some(57563), None),
+
     // -------------------------------------------------------------------------
     // feature-group-end: actual feature gates
     // -------------------------------------------------------------------------
index 496e620dd9df61d10b53e061b1eee67008561b04..1d741085853609e926d7f997f5190b94431a95f0 100644 (file)
@@ -112,6 +112,30 @@ fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
     }
 }
 
+#[derive(Debug)]
+pub struct FloatingPointOp;
+impl NonConstOp for FloatingPointOp {
+    const STOPS_CONST_CHECKING: bool = true;
+
+    fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
+        if ccx.const_kind() == hir::ConstContext::ConstFn {
+            Status::Unstable(sym::const_fn_floating_point_arithmetic)
+        } else {
+            Status::Allowed
+        }
+    }
+
+    fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
+        feature_err(
+            &ccx.tcx.sess.parse_sess,
+            sym::const_fn_floating_point_arithmetic,
+            span,
+            &format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()),
+        )
+        .emit();
+    }
+}
+
 #[derive(Debug)]
 pub struct NonPrimitiveOp;
 impl NonConstOp for NonPrimitiveOp {
index 2d5c6451d1a52d59a421d46a6530da9dafde364c..4447564c8b4c5cba667981341fdee3a6448cf70b 100644 (file)
         const_evaluatable_checked,
         const_extern_fn,
         const_fn,
+        const_fn_floating_point_arithmetic,
         const_fn_transmute,
         const_fn_union,
         const_generics,