Skip to content

Commit 20c1b3f

Browse files
committed
Add a const_eval helper to InterpCx
1 parent 1acbf4b commit 20c1b3f

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_macros::HashStable;
2020
use syntax::source_map::{self, Span, DUMMY_SP};
2121

2222
use super::{
23-
Immediate, MPlaceTy, Machine, MemPlace, Memory, Operand, Place, PlaceTy, ScalarMaybeUndef,
24-
StackPopInfo,
23+
Immediate, MPlaceTy, Machine, MemPlace, Memory, OpTy, Operand, Place, PlaceTy,
24+
ScalarMaybeUndef, StackPopInfo,
2525
};
2626

2727
pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
@@ -754,6 +754,24 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
754754
Ok(())
755755
}
756756

757+
pub(super) fn const_eval(
758+
&self,
759+
gid: GlobalId<'tcx>,
760+
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
761+
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
762+
// and thus don't care about the parameter environment. While we could just use
763+
// `self.param_env`, that would mean we invoke the query to evaluate the static
764+
// with different parameter environments, thus causing the static to be evaluated
765+
// multiple times.
766+
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
767+
ty::ParamEnv::reveal_all()
768+
} else {
769+
self.param_env
770+
};
771+
let val = self.tcx.const_eval(param_env.and(gid))?;
772+
self.eval_const_to_op(val, None)
773+
}
774+
757775
pub fn const_eval_raw(
758776
&self,
759777
gid: GlobalId<'tcx>,

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
118118
| sym::size_of
119119
| sym::type_id
120120
| sym::type_name => {
121-
let val =
122-
self.tcx.const_eval_instance(self.param_env, instance, Some(self.tcx.span))?;
123-
let val = self.eval_const_to_op(val, None)?;
121+
let gid = GlobalId { instance, promoted: None };
122+
let val = self.const_eval(gid)?;
124123
self.copy_op(val, dest)?;
125124
}
126125

src/librustc_mir/interpret/operand.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,28 +578,14 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
578578
ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
579579
ty::ConstKind::Unevaluated(def_id, substs) => {
580580
let instance = self.resolve(def_id, substs)?;
581-
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
582-
// and thus don't care about the parameter environment. While we could just use
583-
// `self.param_env`, that would mean we invoke the query to evaluate the static
584-
// with different parameter environments, thus causing the static to be evaluated
585-
// multiple times.
586-
let param_env = if self.tcx.is_static(def_id) {
587-
ty::ParamEnv::reveal_all()
588-
} else {
589-
self.param_env
590-
};
591581
// We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
592582
// The reason we use `const_eval_raw` everywhere else is to prevent cycles during
593583
// validation, because validation automatically reads through any references, thus
594584
// potentially requiring the current static to be evaluated again. This is not a
595585
// problem here, because we need an operand and operands are always reads.
596586
// FIXME(oli-obk): eliminate all the `const_eval_raw` usages when we get rid of
597587
// `StaticKind` once and for all.
598-
let val =
599-
self.tcx.const_eval(param_env.and(GlobalId { instance, promoted: None }))?;
600-
// "recurse". This is only ever going into a recusion depth of 1, because after
601-
// `const_eval` we don't have `Unevaluated` anymore.
602-
return self.eval_const_to_op(val, layout);
588+
return self.const_eval(GlobalId { instance, promoted: None });
603589
}
604590
ty::ConstKind::Value(val_val) => val_val,
605591
};

0 commit comments

Comments
 (0)