Skip to content

Commit

Permalink
Remove eval_promoted const-prop hack
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleywiser committed Aug 22, 2019
1 parent 7381465 commit f13faf5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 26 deletions.
23 changes: 3 additions & 20 deletions src/librustc_mir/const_eval.rs
Expand Up @@ -49,17 +49,6 @@ pub(crate) fn mk_eval_cx<'mir, 'tcx>(
InterpCx::new(tcx.at(span), param_env, CompileTimeInterpreter::new(), Default::default())
}

pub(crate) fn eval_promoted<'mir, 'tcx>(
tcx: TyCtxt<'tcx>,
cid: GlobalId<'tcx>,
body: &'mir mir::Body<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
let span = tcx.def_span(cid.instance.def_id());
let mut ecx = mk_eval_cx(tcx, span, param_env);
eval_body_using_ecx(&mut ecx, cid, body, param_env)
}

fn op_to_const<'tcx>(
ecx: &CompileTimeEvalContext<'_, 'tcx>,
op: OpTy<'tcx>,
Expand Down Expand Up @@ -360,7 +349,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
}
}
// This is a const fn. Call it.
Ok(Some(match ecx.load_mir(instance.def) {
Ok(Some(match ecx.load_mir(instance.def, None) {
Ok(body) => body,
Err(err) => {
if let err_unsup!(NoMirFor(ref path)) = err.kind {
Expand Down Expand Up @@ -664,14 +653,8 @@ pub fn const_eval_raw_provider<'tcx>(
Default::default()
);

let res = ecx.load_mir(cid.instance.def);
res.map(|body| {
if let Some(index) = cid.promoted {
&tcx.promoted_mir(def_id)[index]
} else {
body
}
}).and_then(
let res = ecx.load_mir(cid.instance.def, cid.promoted);
res.and_then(
|body| eval_body_using_ecx(&mut ecx, cid, body, key.param_env)
).and_then(|place| {
Ok(RawConst {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/interpret/eval_context.rs
Expand Up @@ -294,6 +294,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
pub fn load_mir(
&self,
instance: ty::InstanceDef<'tcx>,
promoted: Option<mir::Promoted>,
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
// do not continue if typeck errors occurred (can only occur in local crate)
let did = instance.def_id();
Expand All @@ -303,7 +304,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
{
throw_inval!(TypeckError)
}
trace!("load mir {:?}", instance);
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
if let Some(promoted) = promoted {
return Ok(&self.tcx.promoted_mir(did)[promoted]);
}
match instance {
ty::InstanceDef::Item(def_id) => if self.tcx.is_mir_available(did) {
Ok(self.tcx.optimized_mir(did))
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_mir/transform/const_prop.rs
Expand Up @@ -27,7 +27,7 @@ use crate::interpret::{
ImmTy, MemoryKind, StackPopCleanup, LocalValue, LocalState,
};
use crate::const_eval::{
CompileTimeInterpreter, error_to_const_error, eval_promoted, mk_eval_cx,
CompileTimeInterpreter, error_to_const_error, mk_eval_cx,
};
use crate::transform::{MirPass, MirSource};

Expand Down Expand Up @@ -297,11 +297,8 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
instance,
promoted: Some(*promoted),
};
// cannot use `const_eval` here, because that would require having the MIR
// for the current function available, but we're producing said MIR right now
let res = self.use_ecx(source_info, |this| {
let body = &this.tcx.promoted_mir(this.source.def_id())[*promoted];
eval_promoted(this.tcx, cid, body, this.param_env)
this.ecx.const_eval_raw(cid)
})?;
trace!("evaluated promoted {:?} to {:?}", promoted, res);
res.into()
Expand Down

0 comments on commit f13faf5

Please sign in to comment.