Deduplicate writing null case of struct wrapped nullable pointers
This commit is contained in:
parent
3464401576
commit
e53e9b9e63
2 changed files with 36 additions and 36 deletions
|
|
@ -767,25 +767,11 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
|||
let operand_ty = self.operand_ty(operand);
|
||||
assert_eq!(self.type_size(operand_ty)?, Some(0));
|
||||
}
|
||||
let (offset, TyAndPacked { ty, packed: _ }) =
|
||||
self.nonnull_offset_and_ty(
|
||||
dest_ty,
|
||||
nndiscr,
|
||||
discrfield_source,
|
||||
)?;
|
||||
// TODO: The packed flag is ignored
|
||||
|
||||
// FIXME(solson)
|
||||
let dest = self.force_allocation(dest)?.to_ptr()?;
|
||||
|
||||
let dest = dest.offset(offset.bytes(), &self)?;
|
||||
let dest_size = self.type_size(ty)?.expect(
|
||||
"bad StructWrappedNullablePointer discrfield",
|
||||
);
|
||||
self.memory.write_maybe_aligned_mut(
|
||||
!nonnull.packed,
|
||||
// The sign does not matter for 0
|
||||
|mem| mem.write_primval(dest, PrimVal::Bytes(0), dest_size, false),
|
||||
self.write_struct_wrapped_null_pointer(
|
||||
dest_ty,
|
||||
nndiscr,
|
||||
discrfield_source,
|
||||
dest,
|
||||
)?;
|
||||
}
|
||||
} else {
|
||||
|
|
@ -1021,6 +1007,33 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn write_struct_wrapped_null_pointer(
|
||||
&mut self,
|
||||
dest_ty: ty::Ty<'tcx>,
|
||||
nndiscr: u64,
|
||||
discrfield_source: &layout::FieldPath,
|
||||
dest: Lvalue,
|
||||
) -> EvalResult<'tcx> {
|
||||
let (offset, TyAndPacked { ty, packed }) = self.nonnull_offset_and_ty(
|
||||
dest_ty,
|
||||
nndiscr,
|
||||
discrfield_source,
|
||||
)?;
|
||||
let nonnull = self.force_allocation(dest)?.to_ptr()?.offset(
|
||||
offset.bytes(),
|
||||
&self,
|
||||
)?;
|
||||
trace!("struct wrapped nullable pointer type: {}", ty);
|
||||
// only the pointer part of a fat pointer is used for this space optimization
|
||||
let discr_size = self.type_size(ty)?.expect(
|
||||
"bad StructWrappedNullablePointer discrfield",
|
||||
);
|
||||
self.memory.write_maybe_aligned_mut(!packed, |mem| {
|
||||
// We're writing 0, signedness does not matter
|
||||
mem.write_primval(nonnull, PrimVal::Bytes(0), discr_size, false)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn type_is_fat_ptr(&self, ty: Ty<'tcx>) -> bool {
|
||||
match ty.sty {
|
||||
ty::TyRawPtr(ref tam) |
|
||||
|
|
|
|||
|
|
@ -11,8 +11,8 @@ use rustc::ty;
|
|||
use rustc::ty::layout::Layout;
|
||||
use rustc::ty::subst::Substs;
|
||||
|
||||
use super::{EvalResult, EvalContext, StackPopCleanup, TyAndPacked, PtrAndAlign, GlobalId, Lvalue,
|
||||
HasMemory, MemoryKind, Machine, PrimVal};
|
||||
use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue,
|
||||
MemoryKind, Machine, PrimVal};
|
||||
|
||||
use syntax::codemap::Span;
|
||||
use syntax::ast::Mutability;
|
||||
|
|
@ -125,26 +125,13 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
|
|||
ref discrfield_source,
|
||||
..
|
||||
} => {
|
||||
// TODO: There's some duplication between here and eval_rvalue_into_lvalue
|
||||
if variant_index as u64 != nndiscr {
|
||||
let (offset, TyAndPacked { ty, packed }) = self.nonnull_offset_and_ty(
|
||||
self.write_struct_wrapped_null_pointer(
|
||||
dest_ty,
|
||||
nndiscr,
|
||||
discrfield_source,
|
||||
dest,
|
||||
)?;
|
||||
let nonnull = self.force_allocation(dest)?.to_ptr()?.offset(
|
||||
offset.bytes(),
|
||||
&self,
|
||||
)?;
|
||||
trace!("struct wrapped nullable pointer type: {}", ty);
|
||||
// only the pointer part of a fat pointer is used for this space optimization
|
||||
let discr_size = self.type_size(ty)?.expect(
|
||||
"bad StructWrappedNullablePointer discrfield",
|
||||
);
|
||||
self.write_maybe_aligned_mut(!packed, |ectx| {
|
||||
// We're writing 0, signedness does not matter
|
||||
ectx.memory.write_primval(nonnull, PrimVal::Bytes(0), discr_size, false)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue