Skip to content

Commit 12bf981

Browse files
authored
Rollup merge of rust-lang#60428 - wesleywiser:refactor_const_eval, r=oli-obk
Refactor `eval_body_using_ecx` so that it doesn't need to query for MIR This is the first step toward removing the `mir` field of `ConstPropagator` which will eventually allow us to actually const propagate in MIR. r? @oli-obk
2 parents a7cbd92 + eadf48e commit 12bf981

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

src/librustc_mir/const_eval.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
5959
) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
6060
let span = tcx.def_span(cid.instance.def_id());
6161
let mut ecx = mk_eval_cx(tcx, span, param_env);
62-
eval_body_using_ecx(&mut ecx, cid, Some(mir), param_env)
62+
eval_body_using_ecx(&mut ecx, cid, mir, param_env)
6363
}
6464

6565
fn mplace_to_const<'tcx>(
@@ -107,37 +107,15 @@ fn op_to_const<'tcx>(
107107
ty::Const { val, ty: op.layout.ty }
108108
}
109109

110-
fn eval_body_and_ecx<'a, 'mir, 'tcx>(
111-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
112-
cid: GlobalId<'tcx>,
113-
mir: Option<&'mir mir::Mir<'tcx>>,
114-
param_env: ty::ParamEnv<'tcx>,
115-
) -> (EvalResult<'tcx, MPlaceTy<'tcx>>, CompileTimeEvalContext<'a, 'mir, 'tcx>) {
116-
// we start out with the best span we have
117-
// and try improving it down the road when more information is available
118-
let span = tcx.def_span(cid.instance.def_id());
119-
let span = mir.map(|mir| mir.span).unwrap_or(span);
120-
let mut ecx = InterpretCx::new(tcx.at(span), param_env, CompileTimeInterpreter::new());
121-
let r = eval_body_using_ecx(&mut ecx, cid, mir, param_env);
122-
(r, ecx)
123-
}
124-
125110
// Returns a pointer to where the result lives
126111
fn eval_body_using_ecx<'mir, 'tcx>(
127112
ecx: &mut CompileTimeEvalContext<'_, 'mir, 'tcx>,
128113
cid: GlobalId<'tcx>,
129-
mir: Option<&'mir mir::Mir<'tcx>>,
114+
mir: &'mir mir::Mir<'tcx>,
130115
param_env: ty::ParamEnv<'tcx>,
131116
) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
132117
debug!("eval_body_using_ecx: {:?}, {:?}", cid, param_env);
133118
let tcx = ecx.tcx.tcx;
134-
let mut mir = match mir {
135-
Some(mir) => mir,
136-
None => ecx.load_mir(cid.instance.def)?,
137-
};
138-
if let Some(index) = cid.promoted {
139-
mir = &mir.promoted[index];
140-
}
141119
let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
142120
assert!(!layout.is_unsized());
143121
let ret = ecx.allocate(layout, MemoryKind::Stack);
@@ -618,8 +596,19 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
618596
return Err(ErrorHandled::Reported);
619597
}
620598

621-
let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
622-
res.and_then(|place| {
599+
let span = tcx.def_span(cid.instance.def_id());
600+
let mut ecx = InterpretCx::new(tcx.at(span), key.param_env, CompileTimeInterpreter::new());
601+
602+
let res = ecx.load_mir(cid.instance.def);
603+
res.map(|mir| {
604+
if let Some(index) = cid.promoted {
605+
&mir.promoted[index]
606+
} else {
607+
mir
608+
}
609+
}).and_then(
610+
|mir| eval_body_using_ecx(&mut ecx, cid, mir, key.param_env)
611+
).and_then(|place| {
623612
Ok(RawConst {
624613
alloc_id: place.to_ptr().expect("we allocated this ptr!").alloc_id,
625614
ty: place.layout.ty

src/librustc_mir/transform/const_prop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
301301
// cannot use `const_eval` here, because that would require having the MIR
302302
// for the current function available, but we're producing said MIR right now
303303
let res = self.use_ecx(source_info, |this| {
304-
eval_promoted(this.tcx, cid, this.mir, this.param_env)
304+
let mir = &this.mir.promoted[promoted];
305+
eval_promoted(this.tcx, cid, mir, this.param_env)
305306
})?;
306307
trace!("evaluated promoted {:?} to {:?}", promoted, res);
307308
Some((res.into(), source_info.span))

0 commit comments

Comments
 (0)