]> git.lizzy.rs Git - rust.git/commitdiff
Default to implicit (not explicit) rules for promotability in `const fn`
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Tue, 1 Sep 2020 19:28:55 +0000 (12:28 -0700)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Thu, 17 Sep 2020 15:39:11 +0000 (08:39 -0700)
compiler/rustc_mir/src/transform/promote_consts.rs

index 1d2295a37dddf10bfadfef0740136e396b5c343a..b6124049579fd92b65ce4c1a47644c7d45c28b3f 100644 (file)
@@ -734,7 +734,14 @@ fn validate_call(
     ) -> Result<(), Unpromotable> {
         let fn_ty = callee.ty(self.body, self.tcx);
 
-        if !self.explicit && self.const_kind.is_none() {
+        // `const` and `static` use the explicit rules for promotion regardless of the `Candidate`,
+        // meaning calls to `const fn` can be promoted.
+        let context_uses_explicit_promotion_rules = matches!(
+            self.const_kind,
+            Some(hir::ConstContext::Static(_) | hir::ConstContext::Const)
+        );
+
+        if !self.explicit && !context_uses_explicit_promotion_rules {
             if let ty::FnDef(def_id, _) = *fn_ty.kind() {
                 // Never promote runtime `const fn` calls of
                 // functions without `#[rustc_promotable]`.