Skip to content

Commit f10ebfe

Browse files
committed
Generalize eval_in_interpreter with a helper trait
1 parent 90acda1 commit f10ebfe

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,36 @@ 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+
/// This function takes the place where the result of the evaluation is stored
298+
/// and prepares it for returning it in the appropriate format needed by the specific
299+
/// evaluation query.
300+
fn make_result<'mir>(
301+
mplace: MPlaceTy<'tcx>,
302+
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
303+
) -> Self;
304+
}
305+
306+
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
307+
fn make_result<'mir>(
308+
mplace: MPlaceTy<'tcx>,
309+
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
310+
) -> Self {
311+
let alloc = take_static_root_alloc(ecx, mplace.ptr().provenance.unwrap().alloc_id());
312+
ecx.tcx.mk_const_alloc(alloc)
313+
}
314+
}
315+
316+
impl<'tcx> InterpretationResult<'tcx> for ConstAlloc<'tcx> {
317+
fn make_result<'mir>(
318+
mplace: MPlaceTy<'tcx>,
319+
_ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
320+
) -> Self {
321+
ConstAlloc { alloc_id: mplace.ptr().provenance.unwrap().alloc_id(), ty: mplace.layout.ty }
322+
}
297323
}
298324

299325
#[instrument(skip(tcx), level = "debug")]
@@ -336,11 +362,11 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
336362
eval_in_interpreter(&mut ecx, cid, is_static)
337363
}
338364

339-
pub fn eval_in_interpreter<'mir, 'tcx>(
365+
fn eval_in_interpreter<'mir, 'tcx, R: InterpretationResult<'tcx>>(
340366
ecx: &mut InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
341367
cid: GlobalId<'tcx>,
342368
is_static: bool,
343-
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
369+
) -> Result<R, ErrorHandled> {
344370
// `is_static` just means "in static", it could still be a promoted!
345371
debug_assert_eq!(is_static, ecx.tcx.static_mutability(cid.instance.def_id()).is_some());
346372

@@ -383,14 +409,12 @@ pub fn eval_in_interpreter<'mir, 'tcx>(
383409

384410
let res = const_validate_mplace(&ecx, &mplace, cid);
385411

386-
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
387-
388412
// Validation failed, report an error.
389413
if let Err(error) = res {
414+
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
390415
Err(const_report_error(&ecx, error, alloc_id))
391416
} else {
392-
// Convert to raw constant
393-
Ok(ConstAlloc { alloc_id, ty: mplace.layout.ty })
417+
Ok(R::make_result(mplace, ecx))
394418
}
395419
}
396420
}

0 commit comments

Comments
 (0)