]> git.lizzy.rs Git - rust.git/commitdiff
Do not call the `const_eval` query in mir interpretation except for caching of nulary...
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Mon, 10 Aug 2020 10:40:14 +0000 (12:40 +0200)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Sat, 19 Sep 2020 08:36:36 +0000 (10:36 +0200)
compiler/rustc_mir/src/const_eval/machine.rs
compiler/rustc_mir/src/interpret/eval_context.rs
compiler/rustc_mir/src/interpret/intrinsics.rs
compiler/rustc_mir/src/interpret/operand.rs

index 02e905505c0f5bbffce70e36081bd934e9dbd7a2..8b0e993f02dc3b04a07589773922eca614ad1a30 100644 (file)
@@ -51,7 +51,7 @@ fn try_eval_const_fn_call(
 
         let gid = GlobalId { instance, promoted: None };
 
-        let place = self.const_eval_raw(gid)?;
+        let place = self.const_eval(gid)?;
 
         self.copy_op(place.into(), dest)?;
 
index b2901d8d6a2592719af1a8cb7b91fe645bda3d5a..2bf14cca877eff7e4008b8ec3f14b60c9dcfbb75 100644 (file)
@@ -20,7 +20,7 @@
 use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
 
 use super::{
-    Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy,
+    Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, Operand, Place, PlaceTy,
     ScalarMaybeUninit, StackPopJump,
 };
 use crate::transform::validate::equal_up_to_regions;
@@ -875,32 +875,7 @@ pub(super) fn deallocate_local(
         Ok(())
     }
 
-    pub(super) fn const_eval(
-        &self,
-        gid: GlobalId<'tcx>,
-        ty: Ty<'tcx>,
-    ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
-        // For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
-        // and thus don't care about the parameter environment. While we could just use
-        // `self.param_env`, that would mean we invoke the query to evaluate the static
-        // with different parameter environments, thus causing the static to be evaluated
-        // multiple times.
-        let param_env = if self.tcx.is_static(gid.instance.def_id()) {
-            ty::ParamEnv::reveal_all()
-        } else {
-            self.param_env
-        };
-        let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
-
-        // Even though `ecx.const_eval` is called from `const_to_op` we can never have a
-        // recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
-        // return `ConstValue::Unevaluated`, which is the only way that `const_to_op` will call
-        // `ecx.const_eval`.
-        let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
-        self.const_to_op(&const_, None)
-    }
-
-    pub fn const_eval_raw(
+    pub fn const_eval(
         &self,
         gid: GlobalId<'tcx>,
     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
index 47ca71d9642ba0f4851c719fa4c36e380af53eea..0664f25e409dcd3c8765233d1763a786a2fe13d7 100644 (file)
@@ -152,7 +152,10 @@ pub fn emulate_intrinsic(
                     sym::type_name => self.tcx.mk_static_str(),
                     _ => bug!("already checked for nullary intrinsics"),
                 };
-                let val = self.const_eval(gid, ty)?;
+                let val =
+                    self.tcx.const_eval_global_id(self.param_env, gid, Some(self.tcx.span))?;
+                let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
+                let val = self.const_to_op(&const_, None)?;
                 self.copy_op(val, dest)?;
             }
 
index 2be771a58ef2b7bd08135e5c098fc3eab1800c84..54c43b518fdddf4e7f0d081e93f6f2dd4bb44260 100644 (file)
@@ -553,11 +553,7 @@ pub(super) fn eval_operands(
             ty::ConstKind::Error(_) => throw_inval!(TypeckError(ErrorReported)),
             ty::ConstKind::Unevaluated(def, substs, promoted) => {
                 let instance = self.resolve(def.did, substs)?;
-                // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
-                // The reason we use `const_eval` here is that there can never be a `ty::ConstKind`
-                // that directly mentions the initializer of a static. Statics are always encoded
-                // as constants with vaule `&STATIC`.
-                return Ok(self.const_eval(GlobalId { instance, promoted }, val.ty)?);
+                return Ok(self.const_eval(GlobalId { instance, promoted })?.into());
             }
             ty::ConstKind::Infer(..)
             | ty::ConstKind::Bound(..)