Skip to content

Commit e53e9b9

Browse files
committed
Deduplicate writing null case of struct wrapped nullable pointers
1 parent 3464401 commit e53e9b9

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -767,25 +767,11 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
767767
let operand_ty = self.operand_ty(operand);
768768
assert_eq!(self.type_size(operand_ty)?, Some(0));
769769
}
770-
let (offset, TyAndPacked { ty, packed: _ }) =
771-
self.nonnull_offset_and_ty(
772-
dest_ty,
773-
nndiscr,
774-
discrfield_source,
775-
)?;
776-
// TODO: The packed flag is ignored
777-
778-
// FIXME(solson)
779-
let dest = self.force_allocation(dest)?.to_ptr()?;
780-
781-
let dest = dest.offset(offset.bytes(), &self)?;
782-
let dest_size = self.type_size(ty)?.expect(
783-
"bad StructWrappedNullablePointer discrfield",
784-
);
785-
self.memory.write_maybe_aligned_mut(
786-
!nonnull.packed,
787-
// The sign does not matter for 0
788-
|mem| mem.write_primval(dest, PrimVal::Bytes(0), dest_size, false),
770+
self.write_struct_wrapped_null_pointer(
771+
dest_ty,
772+
nndiscr,
773+
discrfield_source,
774+
dest,
789775
)?;
790776
}
791777
} else {
@@ -1021,6 +1007,33 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
10211007
Ok(())
10221008
}
10231009

1010+
pub(crate) fn write_struct_wrapped_null_pointer(
1011+
&mut self,
1012+
dest_ty: ty::Ty<'tcx>,
1013+
nndiscr: u64,
1014+
discrfield_source: &layout::FieldPath,
1015+
dest: Lvalue,
1016+
) -> EvalResult<'tcx> {
1017+
let (offset, TyAndPacked { ty, packed }) = self.nonnull_offset_and_ty(
1018+
dest_ty,
1019+
nndiscr,
1020+
discrfield_source,
1021+
)?;
1022+
let nonnull = self.force_allocation(dest)?.to_ptr()?.offset(
1023+
offset.bytes(),
1024+
&self,
1025+
)?;
1026+
trace!("struct wrapped nullable pointer type: {}", ty);
1027+
// only the pointer part of a fat pointer is used for this space optimization
1028+
let discr_size = self.type_size(ty)?.expect(
1029+
"bad StructWrappedNullablePointer discrfield",
1030+
);
1031+
self.memory.write_maybe_aligned_mut(!packed, |mem| {
1032+
// We're writing 0, signedness does not matter
1033+
mem.write_primval(nonnull, PrimVal::Bytes(0), discr_size, false)
1034+
})
1035+
}
1036+
10241037
pub(super) fn type_is_fat_ptr(&self, ty: Ty<'tcx>) -> bool {
10251038
match ty.sty {
10261039
ty::TyRawPtr(ref tam) |

src/librustc_mir/interpret/step.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc::ty;
1111
use rustc::ty::layout::Layout;
1212
use rustc::ty::subst::Substs;
1313

14-
use super::{EvalResult, EvalContext, StackPopCleanup, TyAndPacked, PtrAndAlign, GlobalId, Lvalue,
15-
HasMemory, MemoryKind, Machine, PrimVal};
14+
use super::{EvalResult, EvalContext, StackPopCleanup, PtrAndAlign, GlobalId, Lvalue,
15+
MemoryKind, Machine, PrimVal};
1616

1717
use syntax::codemap::Span;
1818
use syntax::ast::Mutability;
@@ -125,26 +125,13 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
125125
ref discrfield_source,
126126
..
127127
} => {
128-
// TODO: There's some duplication between here and eval_rvalue_into_lvalue
129128
if variant_index as u64 != nndiscr {
130-
let (offset, TyAndPacked { ty, packed }) = self.nonnull_offset_and_ty(
129+
self.write_struct_wrapped_null_pointer(
131130
dest_ty,
132131
nndiscr,
133132
discrfield_source,
133+
dest,
134134
)?;
135-
let nonnull = self.force_allocation(dest)?.to_ptr()?.offset(
136-
offset.bytes(),
137-
&self,
138-
)?;
139-
trace!("struct wrapped nullable pointer type: {}", ty);
140-
// only the pointer part of a fat pointer is used for this space optimization
141-
let discr_size = self.type_size(ty)?.expect(
142-
"bad StructWrappedNullablePointer discrfield",
143-
);
144-
self.write_maybe_aligned_mut(!packed, |ectx| {
145-
// We're writing 0, signedness does not matter
146-
ectx.memory.write_primval(nonnull, PrimVal::Bytes(0), discr_size, false)
147-
})?;
148135
}
149136
}
150137

0 commit comments

Comments
 (0)