Skip to content

Commit ec72e17

Browse files
committed
Fixup a bunch of things for "simplify const value"
1 parent 481ca82 commit ec72e17

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::convert::TryInto;
1515

1616
use rustc::mir;
17-
use rustc::ty::layout::{self, Size, TyLayout, HasDataLayout, IntegerExt};
17+
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt};
1818

1919
use rustc::mir::interpret::{
2020
AllocId,
@@ -338,7 +338,7 @@ impl<'tcx, Tag> OpTy<'tcx, Tag>
338338
// Use the existing layout if given (but sanity check in debug mode),
339339
// or compute the layout.
340340
#[inline(always)]
341-
fn from_known_layout<'tcx>(
341+
pub(super) fn from_known_layout<'tcx>(
342342
layout: Option<TyLayout<'tcx>>,
343343
compute: impl FnOnce() -> EvalResult<'tcx, TyLayout<'tcx>>
344344
) -> EvalResult<'tcx, TyLayout<'tcx>> {
@@ -628,7 +628,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
628628
self.eval_place_to_op(place, layout)?,
629629

630630
Constant(ref constant) => {
631-
self.const_to_mplace(constant.literal)?.into()
631+
let layout = super::operand::from_known_layout(layout, || {
632+
let ty = self.monomorphize(mir_op.ty(self.mir(), *self.tcx), self.substs());
633+
self.layout_of(ty)
634+
})?;
635+
let op = Operand::Indirect(self.const_value_to_mplace(constant.literal.val)?);
636+
OpTy { op, layout }
632637
}
633638
};
634639
trace!("{:?}: {:?}", mir_op, *op);

src/librustc_mir/interpret/place.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -527,34 +527,41 @@ where
527527
}
528528

529529
// Also used e.g. when miri runs into a constant.
530-
pub fn const_to_mplace(
530+
pub fn const_value_to_mplace(
531531
&self,
532-
val: &ty::Const<'tcx>,
533-
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
532+
val: ConstValue<'tcx>,
533+
) -> EvalResult<'tcx, MemPlace<M::PointerTag>> {
534534
trace!("const_value_to_mplace: {:?}", val);
535-
let mplace = match val.val {
535+
match val {
536536
ConstValue::Unevaluated(def_id, substs) => {
537537
let instance = self.resolve(def_id, substs)?;
538538
self.global_to_mplace(GlobalId {
539539
instance,
540540
promoted: None,
541-
})?
541+
})
542542
}
543543
ConstValue::ByRef(id, alloc, offset) => {
544544
// We rely on mutability being set correctly in that allocation to prevent writes
545545
// where none should happen -- and for `static mut`, we copy on demand anyway.
546-
MemPlace::from_ptr(Pointer::new(id, offset), alloc.align).with_default_tag()
546+
Ok(MemPlace::from_ptr(Pointer::new(id, offset), alloc.align).with_default_tag())
547547
}
548-
};
549-
Ok(MPlaceTy { mplace, layout: self.layout_of(val.ty)? })
548+
}
549+
}
550+
551+
pub fn const_to_mplace(
552+
&self,
553+
cnst: &ty::Const<'tcx>,
554+
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
555+
let mplace = self.const_value_to_mplace(cnst.val)?;
556+
Ok(MPlaceTy { mplace, layout: self.layout_of(cnst.ty)? })
550557
}
551558

552559
fn global_to_mplace(
553560
&self,
554561
gid: GlobalId<'tcx>
555562
) -> EvalResult<'tcx, MemPlace<M::PointerTag>> {
556563
let cv = self.const_eval(gid)?;
557-
Ok(*self.const_to_mplace(cv)?)
564+
self.const_value_to_mplace(cv.val)
558565
}
559566

560567
/// Evaluate statics and promoteds to an `MPlace`. Used to share some code between

0 commit comments

Comments
 (0)