Skip to content

Commit caeb3d5

Browse files
committed
Move MIR body loading to a machine function
1 parent f6d54aa commit caeb3d5

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
200200

201201
type MemoryExtra = MemoryExtra;
202202

203+
fn load_mir(
204+
ecx: &InterpCx<'mir, 'tcx, Self>,
205+
instance: ty::InstanceDef<'tcx>,
206+
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
207+
match instance {
208+
ty::InstanceDef::Item(def) => {
209+
if ecx.tcx.is_ctfe_mir_available(def.did) {
210+
Ok(ecx.tcx.mir_for_ctfe_opt_const_arg(def))
211+
} else {
212+
throw_unsup!(NoMirFor(def.did))
213+
}
214+
}
215+
_ => Ok(ecx.tcx.instance_mir(instance)),
216+
}
217+
}
218+
203219
fn find_mir_or_eval_fn(
204220
ecx: &mut InterpCx<'mir, 'tcx, Self>,
205221
instance: ty::Instance<'tcx>,

compiler/rustc_mir/src/interpret/eval_context.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
477477
if let Some(promoted) = promoted {
478478
return Ok(&self.tcx.promoted_mir_opt_const_arg(def)[promoted]);
479479
}
480-
match instance {
481-
ty::InstanceDef::Item(def) => {
482-
if self.tcx.is_ctfe_mir_available(def.did) {
483-
Ok(self.tcx.mir_for_ctfe_opt_const_arg(def))
484-
} else {
485-
throw_unsup!(NoMirFor(def.did))
486-
}
487-
}
488-
_ => Ok(self.tcx.instance_mir(instance)),
489-
}
480+
M::load_mir(self, instance)
490481
}
491482

492483
/// Call this on things you got out of the MIR (so it is as generic as the current

compiler/rustc_mir/src/interpret/machine.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ pub trait Machine<'mir, 'tcx>: Sized {
131131
/// Whether to enforce the validity invariant
132132
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
133133

134+
/// Entry point for obtaining the MIR of anything that should get evaluated.
135+
/// So not just functions and shims, but also const/static initializers, anonymous
136+
/// constants, ...
137+
fn load_mir(
138+
ecx: &InterpCx<'mir, 'tcx, Self>,
139+
instance: ty::InstanceDef<'tcx>,
140+
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
141+
Ok(ecx.tcx.instance_mir(instance))
142+
}
143+
134144
/// Entry point to all function calls.
135145
///
136146
/// Returns either the mir to use for the call, or `None` if execution should

compiler/rustc_mir/src/transform/const_prop.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
184184

185185
type MemoryExtra = ();
186186

187+
fn load_mir(
188+
_ecx: &InterpCx<'mir, 'tcx, Self>,
189+
_instance: ty::InstanceDef<'tcx>,
190+
) -> InterpResult<'tcx, &'tcx Body<'tcx>> {
191+
throw_machine_stop_str!("calling functions isn't supported in ConstProp")
192+
}
193+
187194
fn find_mir_or_eval_fn(
188195
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
189196
_instance: ty::Instance<'tcx>,

0 commit comments

Comments
 (0)