Skip to content

Commit 735a610

Browse files
committed
Only memoize const fn calls during const eval
Miri and other engines may want to execute the function in order to detect UB inside of them.
1 parent 4752c05 commit 735a610

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,20 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
336336
_unwind: Option<mir::BasicBlock> // unwinding is not supported in consts
337337
) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>> {
338338
debug!("eval_fn_call: {:?}", instance);
339+
340+
// If this function is a `const fn` then as an optimization we can query this
341+
// evaluation immediately.
342+
//
343+
// For the moment we only do this for functions which take no arguments
344+
// (or all arguments are ZSTs) so that we don't memoize too much.
345+
if ecx.tcx.is_const_fn_raw(instance.def.def_id()) &&
346+
args.iter().all(|a| a.layout.is_zst())
347+
{
348+
let gid = GlobalId { instance, promoted: None };
349+
ecx.eval_const_fn_call(gid, ret)?;
350+
return Ok(None);
351+
}
352+
339353
// Only check non-glue functions
340354
if let ty::InstanceDef::Item(def_id) = instance.def {
341355
// Execution might have wandered off into other crates, so we cannot do a stability-

src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
284284
ty::InstanceDef::DropGlue(..) |
285285
ty::InstanceDef::CloneShim(..) |
286286
ty::InstanceDef::Item(_) => {
287-
// If this function is a `const fn` then as an optimization we can query this
288-
// evaluation immediately.
289-
//
290-
// For the moment we only do this for functions which take no arguments
291-
// (or all arguments are ZSTs) so that we don't memoize too much.
292-
if self.tcx.is_const_fn_raw(instance.def.def_id()) &&
293-
args.iter().all(|a| a.layout.is_zst())
294-
{
295-
let gid = GlobalId { instance, promoted: None };
296-
return self.eval_const_fn_call(gid, ret);
297-
}
298-
299287
// We need MIR for this fn
300288
let body = match M::find_fn(self, instance, args, ret, unwind)? {
301289
Some(body) => body,
@@ -463,7 +451,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
463451

464452
/// Evaluate a const function where all arguments (if any) are zero-sized types.
465453
/// The evaluation is memoized thanks to the query system.
466-
fn eval_const_fn_call(
454+
pub (crate) fn eval_const_fn_call(
467455
&mut self,
468456
gid: GlobalId<'tcx>,
469457
ret: Option<(PlaceTy<'tcx, M::PointerTag>, mir::BasicBlock)>,

0 commit comments

Comments
 (0)