about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Scherer <github35764891676564198441@oli-obk.de>2019-12-22 20:56:01 +0100
committerOliver Scherer <github35764891676564198441@oli-obk.de>2019-12-26 22:50:18 +0100
commit20c1b3fb49385bed0fc1fc539f573d3695a94753 (patch)
treebe0857468f3bba8a9b59e7dd5ddaa6472e668c79
parent1acbf4b8028ae4eb86128c07945ac0cdc8f7d811 (diff)
downloadrust-20c1b3fb49385bed0fc1fc539f573d3695a94753.tar.gz
rust-20c1b3fb49385bed0fc1fc539f573d3695a94753.zip
Add a `const_eval` helper to `InterpCx`
-rw-r--r--src/librustc_mir/interpret/eval_context.rs22
-rw-r--r--src/librustc_mir/interpret/intrinsics.rs5
-rw-r--r--src/librustc_mir/interpret/operand.rs16
3 files changed, 23 insertions, 20 deletions
diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs
index 76f74a3ecaa..da4156c2719 100644
--- a/src/librustc_mir/interpret/eval_context.rs
+++ b/src/librustc_mir/interpret/eval_context.rs
@@ -20,8 +20,8 @@ use rustc_macros::HashStable;
 use syntax::source_map::{self, Span, DUMMY_SP};
 
 use super::{
-    Immediate, MPlaceTy, Machine, MemPlace, Memory, Operand, Place, PlaceTy, ScalarMaybeUndef,
-    StackPopInfo,
+    Immediate, MPlaceTy, Machine, MemPlace, Memory, OpTy, Operand, Place, PlaceTy,
+    ScalarMaybeUndef, StackPopInfo,
 };
 
 pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
@@ -754,6 +754,24 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         Ok(())
     }
 
+    pub(super) fn const_eval(
+        &self,
+        gid: GlobalId<'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(param_env.and(gid))?;
+        self.eval_const_to_op(val, None)
+    }
+
     pub fn const_eval_raw(
         &self,
         gid: GlobalId<'tcx>,
diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs
index 8e4dc87451c..eb7657f780c 100644
--- a/src/librustc_mir/interpret/intrinsics.rs
+++ b/src/librustc_mir/interpret/intrinsics.rs
@@ -118,9 +118,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
             | sym::size_of
             | sym::type_id
             | sym::type_name => {
-                let val =
-                    self.tcx.const_eval_instance(self.param_env, instance, Some(self.tcx.span))?;
-                let val = self.eval_const_to_op(val, None)?;
+                let gid = GlobalId { instance, promoted: None };
+                let val = self.const_eval(gid)?;
                 self.copy_op(val, dest)?;
             }
 
diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs
index 694ffaa83e4..fc63847433b 100644
--- a/src/librustc_mir/interpret/operand.rs
+++ b/src/librustc_mir/interpret/operand.rs
@@ -578,16 +578,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
             ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
             ty::ConstKind::Unevaluated(def_id, substs) => {
                 let instance = self.resolve(def_id, substs)?;
-                // 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(def_id) {
-                    ty::ParamEnv::reveal_all()
-                } else {
-                    self.param_env
-                };
                 // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
                 // The reason we use `const_eval_raw` everywhere else is to prevent cycles during
                 // validation, because validation automatically reads through any references, thus
@@ -595,11 +585,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
                 // problem here, because we need an operand and operands are always reads.
                 // FIXME(oli-obk): eliminate all the `const_eval_raw` usages when we get rid of
                 // `StaticKind` once and for all.
-                let val =
-                    self.tcx.const_eval(param_env.and(GlobalId { instance, promoted: None }))?;
-                // "recurse". This is only ever going into a recusion depth of 1, because after
-                // `const_eval` we don't have `Unevaluated` anymore.
-                return self.eval_const_to_op(val, layout);
+                return self.const_eval(GlobalId { instance, promoted: None });
             }
             ty::ConstKind::Value(val_val) => val_val,
         };