Skip to content

Commit

Permalink
Small refactorings for miri.
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 committed Aug 2, 2021
1 parent 63ed625 commit a1cff1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
12 changes: 8 additions & 4 deletions compiler/rustc_mir/src/interpret/operand.rs
Expand Up @@ -63,15 +63,19 @@ impl<'tcx, Tag: Provenance> Immediate<Tag> {
Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
}

pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_pointer(vtable, cx))
pub fn new_dyn_trait(
val: Scalar<Tag>,
vtable: Pointer<Option<Tag>>,
cx: &impl HasDataLayout,
) -> Self {
Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_maybe_pointer(vtable, cx))
}

#[inline]
pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit<Tag> {
match self {
Immediate::Scalar(val) => val,
Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"),
Immediate::ScalarPair(..) => bug!("Got a scalar pair where a scalar was expected"),
}
}

Expand All @@ -85,7 +89,7 @@ impl<'tcx, Tag: Provenance> Immediate<Tag> {
match self {
Immediate::ScalarPair(val1, val2) => Ok((val1.check_init()?, val2.check_init()?)),
Immediate::Scalar(..) => {
bug!("Got a scalar where a wide pointer was expected")
bug!("Got a scalar where a scalar pair was expected")
}
}
}
Expand Down
21 changes: 8 additions & 13 deletions compiler/rustc_mir/src/interpret/traits.rs
Expand Up @@ -21,7 +21,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&mut self,
ty: Ty<'tcx>,
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
trace!("get_vtable(trait_ref={:?})", poly_trait_ref);

let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref));
Expand All @@ -34,7 +34,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

let vtable_ptr = self.memory.global_base_pointer(Pointer::from(vtable_allocation))?;

Ok(vtable_ptr)
Ok(vtable_ptr.into())
}

/// Resolves the function at the specified slot in the provided
Expand Down Expand Up @@ -126,21 +126,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&self,
vtable: Pointer<Option<M::PointerTag>>,
idx: u64,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
let pointer_size = self.pointer_size();

let vtable = self
let vtable_slot = vtable.offset(pointer_size * idx, self)?;
let new_vtable = self
.memory
.get(
vtable,
pointer_size * idx.checked_add(1).unwrap(),
self.tcx.data_layout.pointer_align.abi,
)?
.get(vtable_slot, pointer_size, self.tcx.data_layout.pointer_align.abi)?
.expect("cannot be a ZST");
let new_vtable = self
.scalar_to_ptr(vtable.read_ptr_sized(pointer_size * idx)?.check_init()?)
.into_pointer_or_addr()
.expect("should be a pointer");

let new_vtable = self.scalar_to_ptr(new_vtable.read_ptr_sized(Size::ZERO)?.check_init()?);

Ok(new_vtable)
}
Expand Down

0 comments on commit a1cff1c

Please sign in to comment.