From a1cff1cd4990a4ee8c5df8d7c71c05a0bcd60dec Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Sun, 1 Aug 2021 20:09:22 +0800 Subject: [PATCH] Small refactorings for miri. --- compiler/rustc_mir/src/interpret/operand.rs | 12 +++++++---- compiler/rustc_mir/src/interpret/traits.rs | 23 ++++++++------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_mir/src/interpret/operand.rs b/compiler/rustc_mir/src/interpret/operand.rs index bd6ee4a37e3..d5bc2b1e2ea 100644 --- a/compiler/rustc_mir/src/interpret/operand.rs +++ b/compiler/rustc_mir/src/interpret/operand.rs @@ -63,15 +63,19 @@ impl<'tcx, Tag: Provenance> Immediate { Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into()) } - pub fn new_dyn_trait(val: Scalar, vtable: Pointer, cx: &impl HasDataLayout) -> Self { - Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_pointer(vtable, cx)) + pub fn new_dyn_trait( + val: Scalar, + vtable: Pointer>, + cx: &impl HasDataLayout, + ) -> Self { + Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_maybe_pointer(vtable, cx)) } #[inline] pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit { 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"), } } @@ -85,7 +89,7 @@ impl<'tcx, Tag: Provenance> Immediate { 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") } } } diff --git a/compiler/rustc_mir/src/interpret/traits.rs b/compiler/rustc_mir/src/interpret/traits.rs index 041be9814a3..a6ba00ec695 100644 --- a/compiler/rustc_mir/src/interpret/traits.rs +++ b/compiler/rustc_mir/src/interpret/traits.rs @@ -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>, - ) -> InterpResult<'tcx, Pointer> { + ) -> InterpResult<'tcx, Pointer>> { trace!("get_vtable(trait_ref={:?})", poly_trait_ref); let (ty, poly_trait_ref) = self.tcx.erase_regions((ty, poly_trait_ref)); @@ -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 @@ -126,21 +126,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { &self, vtable: Pointer>, idx: u64, - ) -> InterpResult<'tcx, Pointer> { + ) -> InterpResult<'tcx, Pointer>> { let pointer_size = self.pointer_size(); - let vtable = self - .memory - .get( - vtable, - pointer_size * idx.checked_add(1).unwrap(), - self.tcx.data_layout.pointer_align.abi, - )? - .expect("cannot be a ZST"); + let vtable_slot = vtable.offset(pointer_size * idx, self)?; 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"); + .memory + .get(vtable_slot, pointer_size, self.tcx.data_layout.pointer_align.abi)? + .expect("cannot be a ZST"); + + let new_vtable = self.scalar_to_ptr(new_vtable.read_ptr_sized(Size::ZERO)?.check_init()?); Ok(new_vtable) }