Pass ImmTy by reference not value

This commit is contained in:
Tomasz Miąsko 2021-02-15 00:00:00 +00:00
parent fe0c46d07e
commit 5888556efe
5 changed files with 10 additions and 10 deletions

View File

@ -32,7 +32,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Misc => { Misc => {
let src = self.read_immediate(src)?; let src = self.read_immediate(src)?;
let res = self.misc_cast(src, cast_ty)?; let res = self.misc_cast(&src, cast_ty)?;
self.write_immediate(res, dest)?; self.write_immediate(res, dest)?;
} }
@ -107,7 +107,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
fn misc_cast( fn misc_cast(
&self, &self,
src: ImmTy<'tcx, M::PointerTag>, src: &ImmTy<'tcx, M::PointerTag>,
cast_ty: Ty<'tcx>, cast_ty: Ty<'tcx>,
) -> InterpResult<'tcx, Immediate<M::PointerTag>> { ) -> InterpResult<'tcx, Immediate<M::PointerTag>> {
use rustc_middle::ty::TyKind::*; use rustc_middle::ty::TyKind::*;
@ -158,13 +158,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let dest_layout = self.layout_of(cast_ty)?; let dest_layout = self.layout_of(cast_ty)?;
if dest_layout.size == src.layout.size { if dest_layout.size == src.layout.size {
// Thin or fat pointer that just hast the ptr kind of target type changed. // Thin or fat pointer that just hast the ptr kind of target type changed.
return Ok(*src); return Ok(**src);
} else { } else {
// Casting the metadata away from a fat ptr. // Casting the metadata away from a fat ptr.
assert_eq!(src.layout.size, 2 * self.memory.pointer_size()); assert_eq!(src.layout.size, 2 * self.memory.pointer_size());
assert_eq!(dest_layout.size, self.memory.pointer_size()); assert_eq!(dest_layout.size, self.memory.pointer_size());
assert!(src.layout.ty.is_unsafe_ptr()); assert!(src.layout.ty.is_unsafe_ptr());
return match *src { return match **src {
Immediate::ScalarPair(data, _) => Ok(data.into()), Immediate::ScalarPair(data, _) => Ok(data.into()),
Immediate::Scalar(..) => span_bug!( Immediate::Scalar(..) => span_bug!(
self.cur_span(), self.cur_span(),

View File

@ -198,7 +198,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
let ty = mplace.layout.ty; let ty = mplace.layout.ty;
if let ty::Ref(_, referenced_ty, ref_mutability) = *ty.kind() { if let ty::Ref(_, referenced_ty, ref_mutability) = *ty.kind() {
let value = self.ecx.read_immediate(&(*mplace).into())?; let value = self.ecx.read_immediate(&(*mplace).into())?;
let mplace = self.ecx.ref_to_mplace(value)?; let mplace = self.ecx.ref_to_mplace(&value)?;
assert_eq!(mplace.layout.ty, referenced_ty); assert_eq!(mplace.layout.ty, referenced_ty);
// Handle trait object vtables. // Handle trait object vtables.
if let ty::Dynamic(..) = if let ty::Dynamic(..) =

View File

@ -143,7 +143,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
sym::min_align_of_val | sym::size_of_val => { sym::min_align_of_val | sym::size_of_val => {
// Avoid `deref_operand` -- this is not a deref, the ptr does not have to be // Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
// dereferencable! // dereferencable!
let place = self.ref_to_mplace(self.read_immediate(&args[0])?)?; let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
let (size, align) = self let (size, align) = self
.size_and_align_of_mplace(place)? .size_and_align_of_mplace(place)?
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?; .ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;

View File

@ -303,12 +303,12 @@ where
/// Generally prefer `deref_operand`. /// Generally prefer `deref_operand`.
pub fn ref_to_mplace( pub fn ref_to_mplace(
&self, &self,
val: ImmTy<'tcx, M::PointerTag>, val: &ImmTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
let pointee_type = let pointee_type =
val.layout.ty.builtin_deref(true).expect("`ref_to_mplace` called on non-ptr type").ty; val.layout.ty.builtin_deref(true).expect("`ref_to_mplace` called on non-ptr type").ty;
let layout = self.layout_of(pointee_type)?; let layout = self.layout_of(pointee_type)?;
let (ptr, meta) = match *val { let (ptr, meta) = match **val {
Immediate::Scalar(ptr) => (ptr.check_init()?, MemPlaceMeta::None), Immediate::Scalar(ptr) => (ptr.check_init()?, MemPlaceMeta::None),
Immediate::ScalarPair(ptr, meta) => { Immediate::ScalarPair(ptr, meta) => {
(ptr.check_init()?, MemPlaceMeta::Meta(meta.check_init()?)) (ptr.check_init()?, MemPlaceMeta::Meta(meta.check_init()?))
@ -335,7 +335,7 @@ where
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> { ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
let val = self.read_immediate(src)?; let val = self.read_immediate(src)?;
trace!("deref to {} on {:?}", val.layout.ty, *val); trace!("deref to {} on {:?}", val.layout.ty, *val);
let place = self.ref_to_mplace(val)?; let place = self.ref_to_mplace(&val)?;
self.mplace_access_checked(place, None) self.mplace_access_checked(place, None)
} }

View File

@ -386,7 +386,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
// Handle wide pointers. // Handle wide pointers.
// Check metadata early, for better diagnostics // Check metadata early, for better diagnostics
let place = try_validation!( let place = try_validation!(
self.ecx.ref_to_mplace(value), self.ecx.ref_to_mplace(&value),
self.path, self.path,
err_ub!(InvalidUninitBytes(None)) => { "uninitialized {}", kind }, err_ub!(InvalidUninitBytes(None)) => { "uninitialized {}", kind },
); );