Skip to content

Commit 3e1addf

Browse files
committed
Share the InterpCx creation between static and const evaluation
1 parent 84b0073 commit 3e1addf

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,7 @@ pub fn eval_static_initializer_provider<'tcx>(
282282

283283
let instance = ty::Instance::mono(tcx, def_id.to_def_id());
284284
let cid = rustc_middle::mir::interpret::GlobalId { instance, promoted: None };
285-
let ecx = InterpCx::new(
286-
tcx,
287-
tcx.def_span(def_id),
288-
ty::ParamEnv::reveal_all(),
289-
// Statics (and promoteds inside statics) may access other statics, because unlike consts
290-
// they do not have to behave "as if" they were evaluated at runtime.
291-
CompileTimeInterpreter::new(CanAccessMutGlobal::Yes, CheckAlignment::Error),
292-
);
293-
eval_in_interpreter(ecx, cid)
285+
eval_in_interpreter(tcx, cid, ty::ParamEnv::reveal_all())
294286
}
295287

296288
pub trait InterpretationResult<'tcx> {
@@ -332,27 +324,27 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
332324
trace!("const eval: {:?} ({})", key, instance);
333325
}
334326

335-
let cid = key.value;
327+
eval_in_interpreter(tcx, key.value, key.param_env)
328+
}
329+
330+
fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
331+
tcx: TyCtxt<'tcx>,
332+
cid: GlobalId<'tcx>,
333+
param_env: ty::ParamEnv<'tcx>,
334+
) -> Result<R, ErrorHandled> {
336335
let def = cid.instance.def.def_id();
337336
let is_static = tcx.is_static(def);
338337

339-
let ecx = InterpCx::new(
338+
let mut ecx = InterpCx::new(
340339
tcx,
341340
tcx.def_span(def),
342-
key.param_env,
341+
param_env,
343342
// Statics (and promoteds inside statics) may access mutable global memory, because unlike consts
344343
// they do not have to behave "as if" they were evaluated at runtime.
345344
// For consts however we want to ensure they behave "as if" they were evaluated at runtime,
346345
// so we have to reject reading mutable global memory.
347346
CompileTimeInterpreter::new(CanAccessMutGlobal::from(is_static), CheckAlignment::Error),
348347
);
349-
eval_in_interpreter(ecx, cid)
350-
}
351-
352-
fn eval_in_interpreter<'mir, 'tcx, R: InterpretationResult<'tcx>>(
353-
mut ecx: InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
354-
cid: GlobalId<'tcx>,
355-
) -> Result<R, ErrorHandled> {
356348
let res = ecx.load_mir(cid.instance.def, cid.promoted);
357349
match res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, body)) {
358350
Err(error) => {

0 commit comments

Comments
 (0)