Skip to content

Commit dd9702a

Browse files
committed
Do not call the const_eval query in mir interpretation except for caching of nulary intrinsics
1 parent a6c60bb commit dd9702a

File tree

4 files changed

+8
-34
lines changed

4 files changed

+8
-34
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
5151

5252
let gid = GlobalId { instance, promoted: None };
5353

54-
let place = self.const_eval_raw(gid)?;
54+
let place = self.const_eval(gid)?;
5555

5656
self.copy_op(place.into(), dest)?;
5757

compiler/rustc_mir/src/interpret/eval_context.rs

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::{Pos, Span};
2020
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
2121

2222
use super::{
23-
Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy,
23+
Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, Operand, Place, PlaceTy,
2424
ScalarMaybeUninit, StackPopJump,
2525
};
2626
use crate::transform::validate::equal_up_to_regions;
@@ -875,32 +875,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
875875
Ok(())
876876
}
877877

878-
pub(super) fn const_eval(
879-
&self,
880-
gid: GlobalId<'tcx>,
881-
ty: Ty<'tcx>,
882-
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
883-
// For statics we pick `ParamEnv::reveal_all`, because statics don't have generics
884-
// and thus don't care about the parameter environment. While we could just use
885-
// `self.param_env`, that would mean we invoke the query to evaluate the static
886-
// with different parameter environments, thus causing the static to be evaluated
887-
// multiple times.
888-
let param_env = if self.tcx.is_static(gid.instance.def_id()) {
889-
ty::ParamEnv::reveal_all()
890-
} else {
891-
self.param_env
892-
};
893-
let val = self.tcx.const_eval_global_id(param_env, gid, Some(self.tcx.span))?;
894-
895-
// Even though `ecx.const_eval` is called from `const_to_op` we can never have a
896-
// recursion deeper than one level, because the `tcx.const_eval` above is guaranteed to not
897-
// return `ConstValue::Unevaluated`, which is the only way that `const_to_op` will call
898-
// `ecx.const_eval`.
899-
let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
900-
self.const_to_op(&const_, None)
901-
}
902-
903-
pub fn const_eval_raw(
878+
pub fn const_eval(
904879
&self,
905880
gid: GlobalId<'tcx>,
906881
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {

compiler/rustc_mir/src/interpret/intrinsics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
152152
sym::type_name => self.tcx.mk_static_str(),
153153
_ => bug!("already checked for nullary intrinsics"),
154154
};
155-
let val = self.const_eval(gid, ty)?;
155+
let val =
156+
self.tcx.const_eval_global_id(self.param_env, gid, Some(self.tcx.span))?;
157+
let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
158+
let val = self.const_to_op(&const_, None)?;
156159
self.copy_op(val, dest)?;
157160
}
158161

compiler/rustc_mir/src/interpret/operand.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,11 +553,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
553553
ty::ConstKind::Error(_) => throw_inval!(TypeckError(ErrorReported)),
554554
ty::ConstKind::Unevaluated(def, substs, promoted) => {
555555
let instance = self.resolve(def.did, substs)?;
556-
// We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
557-
// The reason we use `const_eval` here is that there can never be a `ty::ConstKind`
558-
// that directly mentions the initializer of a static. Statics are always encoded
559-
// as constants with vaule `&STATIC`.
560-
return Ok(self.const_eval(GlobalId { instance, promoted }, val.ty)?);
556+
return Ok(self.const_eval(GlobalId { instance, promoted })?.into());
561557
}
562558
ty::ConstKind::Infer(..)
563559
| ty::ConstKind::Bound(..)

0 commit comments

Comments
 (0)