Skip to content

Commit a50e459

Browse files
committed
Generalize eval_in_interpreter with a helper trait
1 parent 3b85d2c commit a50e459

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,33 @@ pub fn eval_static_initializer_provider<'tcx>(
290290
// they do not have to behave "as if" they were evaluated at runtime.
291291
CompileTimeInterpreter::new(CanAccessMutGlobal::Yes, CheckAlignment::Error),
292292
);
293-
let alloc_id = eval_in_interpreter(&mut ecx, cid, true)?.alloc_id;
294-
let alloc = take_static_root_alloc(&mut ecx, alloc_id);
295-
let alloc = tcx.mk_const_alloc(alloc);
296-
Ok(alloc)
293+
eval_in_interpreter(&mut ecx, cid, true)
294+
}
295+
296+
trait InterpretationResult<'tcx> {
297+
fn make_result<'mir>(
298+
mplace: MPlaceTy<'tcx>,
299+
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
300+
) -> Self;
301+
}
302+
303+
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
304+
fn make_result<'mir>(
305+
mplace: MPlaceTy<'tcx>,
306+
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
307+
) -> Self {
308+
let alloc = take_static_root_alloc(ecx, mplace.ptr().provenance.unwrap().alloc_id());
309+
ecx.tcx.mk_const_alloc(alloc)
310+
}
311+
}
312+
313+
impl<'tcx> InterpretationResult<'tcx> for ConstAlloc<'tcx> {
314+
fn make_result<'mir>(
315+
mplace: MPlaceTy<'tcx>,
316+
_ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
317+
) -> Self {
318+
ConstAlloc { alloc_id: mplace.ptr().provenance.unwrap().alloc_id(), ty: mplace.layout.ty }
319+
}
297320
}
298321

299322
#[instrument(skip(tcx), level = "debug")]
@@ -336,11 +359,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
336359
eval_in_interpreter(&mut ecx, cid, is_static)
337360
}
338361

339-
pub fn eval_in_interpreter<'mir, 'tcx>(
362+
fn eval_in_interpreter<'mir, 'tcx, R: InterpretationResult<'tcx>>(
340363
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
341364
cid: GlobalId<'tcx>,
342365
is_static: bool,
343-
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
366+
) -> Result<R, ErrorHandled> {
344367
// `is_static` just means "in static", it could still be a promoted!
345368
debug_assert_eq!(is_static, ecx.tcx.static_mutability(cid.instance.def_id()).is_some());
346369

@@ -386,14 +409,12 @@ pub fn eval_in_interpreter<'mir, 'tcx>(
386409
let res = const_validate_mplace(&ecx, &mplace, cid);
387410
ecx.machine.static_root_ids = static_root_ids;
388411

389-
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
390-
391412
// Validation failed, report an error.
392413
if let Err(error) = res {
414+
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
393415
Err(const_report_error(&ecx, error, alloc_id))
394416
} else {
395-
// Convert to raw constant
396-
Ok(ConstAlloc { alloc_id, ty: mplace.layout.ty })
417+
Ok(R::make_result(mplace, ecx))
397418
}
398419
}
399420
}

0 commit comments

Comments
 (0)