Skip to content

Commit cc7e0b2

Browse files
committed
Share the InterpCx creation between static and const evaluation
1 parent 02a0ac8 commit cc7e0b2

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> {
@@ -335,27 +327,27 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
335327
trace!("const eval: {:?} ({})", key, instance);
336328
}
337329

338-
let cid = key.value;
330+
eval_in_interpreter(tcx, key.value, key.param_env)
331+
}
332+
333+
fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
334+
tcx: TyCtxt<'tcx>,
335+
cid: GlobalId<'tcx>,
336+
param_env: ty::ParamEnv<'tcx>,
337+
) -> Result<R, ErrorHandled> {
339338
let def = cid.instance.def.def_id();
340339
let is_static = tcx.is_static(def);
341340

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

0 commit comments

Comments
 (0)