Skip to content

Commit d4196a7

Browse files
RalfJungeddyb
authored andcommitted
start cleaning up subst mess
fix an ICE fix method name
1 parent cdff918 commit d4196a7

File tree

6 files changed

+38
-58
lines changed

6 files changed

+38
-58
lines changed

src/librustc_mir/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6767
// The src operand does not matter, just its type
6868
match src.layout.ty.sty {
6969
ty::Closure(def_id, substs) => {
70-
let substs = self.subst_and_normalize_erasing_regions(substs)?;
70+
let substs = self.subst_from_frame_and_normalize_erasing_regions(substs)?;
7171
let instance = ty::Instance::resolve_closure(
7272
*self.tcx,
7373
def_id,

src/librustc_mir/interpret/eval_context.rs

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::mir;
99
use rustc::ty::layout::{
1010
self, Size, Align, HasDataLayout, LayoutOf, TyLayout
1111
};
12-
use rustc::ty::subst::{Subst, SubstsRef};
12+
use rustc::ty::subst::SubstsRef;
1313
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
1414
use rustc::ty::query::TyCtxtAt;
1515
use rustc_data_structures::indexed_vec::IndexVec;
@@ -291,41 +291,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
291291
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
292292
}
293293

294-
pub(super) fn subst_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
295-
&self,
296-
substs: T,
297-
) -> InterpResult<'tcx, T> {
298-
match self.stack.last() {
299-
Some(frame) => Ok(self.tcx.subst_and_normalize_erasing_regions(
300-
frame.instance.substs,
301-
self.param_env,
302-
&substs,
303-
)),
304-
None => if substs.needs_subst() {
305-
throw_inval!(TooGeneric)
306-
} else {
307-
Ok(substs)
308-
},
309-
}
310-
}
311-
312-
pub(super) fn resolve(
313-
&self,
314-
def_id: DefId,
315-
substs: SubstsRef<'tcx>
316-
) -> InterpResult<'tcx, ty::Instance<'tcx>> {
317-
trace!("resolve: {:?}, {:#?}", def_id, substs);
318-
trace!("param_env: {:#?}", self.param_env);
319-
let substs = self.subst_and_normalize_erasing_regions(substs)?;
320-
trace!("substs: {:#?}", substs);
321-
ty::Instance::resolve(
322-
*self.tcx,
323-
self.param_env,
324-
def_id,
325-
substs,
326-
).ok_or_else(|| err_inval!(TooGeneric).into())
327-
}
328-
329294
pub fn load_mir(
330295
&self,
331296
instance: ty::InstanceDef<'tcx>,
@@ -349,12 +314,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
349314
}
350315
}
351316

352-
pub(super) fn monomorphize<T: TypeFoldable<'tcx> + Subst<'tcx>>(
317+
/// Call this on things you got out of the MIR (so it is as generic as the current
318+
/// stack rameframe), to bring it into the proper environment for this interpreter.
319+
pub(super) fn subst_from_frame_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
353320
&self,
354321
t: T,
355322
) -> InterpResult<'tcx, T> {
356323
match self.stack.last() {
357-
Some(frame) => Ok(self.monomorphize_with_substs(t, frame.instance.substs)?),
324+
Some(frame) => Ok(self.tcx.subst_and_normalize_erasing_regions(
325+
frame.instance.substs,
326+
self.param_env,
327+
&t,
328+
)),
358329
None => if t.needs_subst() {
359330
throw_inval!(TooGeneric)
360331
} else {
@@ -363,20 +334,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
363334
}
364335
}
365336

366-
fn monomorphize_with_substs<T: TypeFoldable<'tcx> + Subst<'tcx>>(
337+
/// The `substs` are assumed to already be in our interpreter "universe" (param_env).
338+
pub(super) fn resolve(
367339
&self,
368-
t: T,
340+
def_id: DefId,
369341
substs: SubstsRef<'tcx>
370-
) -> InterpResult<'tcx, T> {
371-
// miri doesn't care about lifetimes, and will choke on some crazy ones
372-
// let's simply get rid of them
373-
let substituted = t.subst(*self.tcx, substs);
374-
375-
if substituted.needs_subst() {
376-
throw_inval!(TooGeneric)
377-
}
378-
379-
Ok(self.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substituted))
342+
) -> InterpResult<'tcx, ty::Instance<'tcx>> {
343+
trace!("resolve: {:?}, {:#?}", def_id, substs);
344+
trace!("param_env: {:#?}", self.param_env);
345+
trace!("substs: {:#?}", substs);
346+
ty::Instance::resolve(
347+
*self.tcx,
348+
self.param_env,
349+
def_id,
350+
substs,
351+
).ok_or_else(|| err_inval!(TooGeneric).into())
380352
}
381353

382354
pub fn layout_of_local(
@@ -391,7 +363,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
391363
None => {
392364
let layout = crate::interpret::operand::from_known_layout(layout, || {
393365
let local_ty = frame.body.local_decls[local].ty;
394-
let local_ty = self.monomorphize_with_substs(local_ty, frame.instance.substs)?;
366+
let local_ty = self.tcx.subst_and_normalize_erasing_regions(
367+
frame.instance.substs,
368+
self.param_env,
369+
&local_ty,
370+
);
395371
self.layout_of(local_ty)
396372
})?;
397373
if let Some(state) = frame.locals.get(local) {

src/librustc_mir/interpret/operand.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
540540

541541
// Used when the miri-engine runs into a constant and for extracting information from constants
542542
// in patterns via the `const_eval` module
543+
/// The `val` and `layout` are assumed to already be in our interpreter
544+
/// "universe" (param_env).
543545
crate fn eval_const_to_op(
544546
&self,
545547
val: &'tcx ty::Const<'tcx>,
@@ -552,7 +554,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
552554
// Early-return cases.
553555
match val.val {
554556
ConstValue::Param(_) =>
555-
// FIXME(oli-obk): try to monomorphize
556557
throw_inval!(TooGeneric),
557558
ConstValue::Unevaluated(def_id, substs) => {
558559
let instance = self.resolve(def_id, substs)?;
@@ -565,7 +566,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
565566
}
566567
// Other cases need layout.
567568
let layout = from_known_layout(layout, || {
568-
self.layout_of(self.monomorphize(val.ty)?)
569+
self.layout_of(val.ty)
569570
})?;
570571
let op = match val.val {
571572
ConstValue::ByRef { alloc, offset } => {

src/librustc_mir/interpret/place.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,11 @@ where
640640
// their layout on return.
641641
PlaceTy {
642642
place: *return_place,
643-
layout: self
644-
.layout_of(self.monomorphize(self.frame().body.return_ty())?)?,
643+
layout: self.layout_of(
644+
self.subst_from_frame_and_normalize_erasing_regions(
645+
self.frame().body.return_ty()
646+
)?
647+
)?,
645648
}
646649
}
647650
None => throw_unsup!(InvalidNullPointerUsage),

src/librustc_mir/interpret/step.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
254254
}
255255

256256
NullaryOp(mir::NullOp::SizeOf, ty) => {
257-
let ty = self.monomorphize(ty)?;
257+
let ty = self.subst_from_frame_and_normalize_erasing_regions(ty)?;
258258
let layout = self.layout_of(ty)?;
259259
assert!(!layout.is_unsized(),
260260
"SizeOf nullary MIR operator called for unsized type");

src/librustc_mir/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
7777
for (i, method) in methods.iter().enumerate() {
7878
if let Some((def_id, substs)) = *method {
7979
// resolve for vtable: insert shims where needed
80-
let substs = self.subst_and_normalize_erasing_regions(substs)?;
80+
let substs = self.subst_from_frame_and_normalize_erasing_regions(substs)?;
8181
let instance = ty::Instance::resolve_for_vtable(
8282
*self.tcx,
8383
self.param_env,

0 commit comments

Comments
 (0)