Skip to content

Commit 026214c

Browse files
committed
rustc: collapse Layout::FatPointer into Layout::Univariant.
1 parent 3fd6b00 commit 026214c

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

src/librustc/ty/layout.rs

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,6 @@ pub enum Layout {
849849
/// TyArray, TySlice or TyStr.
850850
Array,
851851

852-
/// TyRawPtr or TyRef with a !Sized pointee. The primitive is the metadata.
853-
FatPointer,
854-
855852
// Remaining variants are all ADTs such as structs, enums or tuples.
856853

857854
/// Single-case enums, and structs/tuples.
@@ -1132,7 +1129,7 @@ impl<'a, 'tcx> Layout {
11321129
memory_index: vec![0, 1]
11331130
};
11341131
Ok(tcx.intern_layout(CachedLayout {
1135-
layout: Layout::FatPointer,
1132+
layout: Layout::Univariant,
11361133
fields,
11371134
abi: Abi::Aggregate {
11381135
sized: true,
@@ -1743,8 +1740,7 @@ impl<'a, 'tcx> Layout {
17431740
// via representation tweaks) size info beyond total size.
17441741
Layout::Scalar |
17451742
Layout::Vector |
1746-
Layout::Array |
1747-
Layout::FatPointer { .. } => {
1743+
Layout::Array => {
17481744
debug!("print-type-size t: `{:?}` adt other", ty);
17491745
record(adt_kind.into(), None, Vec::new())
17501746
}
@@ -2047,17 +2043,37 @@ impl<'a, 'tcx> FullLayout<'tcx> {
20472043
fn field_type_unnormalized(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, i: usize) -> Ty<'tcx> {
20482044
let ptr_field_type = |pointee: Ty<'tcx>| {
20492045
assert!(i < 2);
2046+
let mk_ptr = |ty: Ty<'tcx>| {
2047+
match self.ty.sty {
2048+
ty::TyRef(r, ty::TypeAndMut { mutbl, .. }) => {
2049+
tcx.mk_ref(r, ty::TypeAndMut { ty, mutbl })
2050+
}
2051+
ty::TyRawPtr(ty::TypeAndMut { mutbl, .. }) => {
2052+
tcx.mk_ptr(ty::TypeAndMut { ty, mutbl })
2053+
}
2054+
ty::TyAdt(def, _) if def.is_box() => {
2055+
tcx.mk_box(ty)
2056+
}
2057+
_ => bug!()
2058+
}
2059+
};
20502060
let slice = |element: Ty<'tcx>| {
20512061
if i == 0 {
2052-
tcx.mk_mut_ptr(element)
2062+
mk_ptr(element)
20532063
} else {
20542064
tcx.types.usize
20552065
}
20562066
};
20572067
match tcx.struct_tail(pointee).sty {
20582068
ty::TySlice(element) => slice(element),
20592069
ty::TyStr => slice(tcx.types.u8),
2060-
ty::TyDynamic(..) => Pointer.to_ty(tcx),
2070+
ty::TyDynamic(..) => {
2071+
if i == 0 {
2072+
mk_ptr(tcx.mk_nil())
2073+
} else {
2074+
Pointer.to_ty(tcx)
2075+
}
2076+
}
20612077
_ => bug!("FullLayout::field_type({:?}): not applicable", self)
20622078
}
20632079
};
@@ -2187,9 +2203,16 @@ impl<'a, 'tcx> FullLayout<'tcx> {
21872203
{
21882204
let tcx = cx.tcx();
21892205
match (self.layout, self.abi, &self.ty.sty) {
2190-
(&Layout::Scalar, Abi::Scalar(Pointer), _) if !self.ty.is_unsafe_ptr() => {
2206+
// FIXME(eddyb) check this via value ranges on scalars.
2207+
(&Layout::Scalar, Abi::Scalar(Pointer), &ty::TyRef(..)) |
2208+
(&Layout::Scalar, Abi::Scalar(Pointer), &ty::TyFnPtr(..)) => {
2209+
Ok(Some((Size::from_bytes(0), Pointer)))
2210+
}
2211+
(&Layout::Scalar, Abi::Scalar(Pointer), &ty::TyAdt(def, _)) if def.is_box() => {
21912212
Ok(Some((Size::from_bytes(0), Pointer)))
21922213
}
2214+
2215+
// FIXME(eddyb) check this via value ranges on scalars.
21932216
(&Layout::General { discr, .. }, _, &ty::TyAdt(def, _)) => {
21942217
if def.discriminants(tcx).all(|d| d.to_u128_unchecked() != 0) {
21952218
Ok(Some((self.fields.offset(0), discr)))
@@ -2198,28 +2221,28 @@ impl<'a, 'tcx> FullLayout<'tcx> {
21982221
}
21992222
}
22002223

2201-
(&Layout::FatPointer, _, _) if !self.ty.is_unsafe_ptr() => {
2202-
Ok(Some((self.fields.offset(FAT_PTR_ADDR), Pointer)))
2203-
}
2204-
22052224
// Is this the NonZero lang item wrapping a pointer or integer type?
22062225
(_, _, &ty::TyAdt(def, _)) if Some(def.did) == tcx.lang_items().non_zero() => {
22072226
let field = self.field(cx, 0)?;
2208-
match (field.layout, field.abi) {
2209-
(&Layout::Scalar, Abi::Scalar(value)) => {
2210-
Ok(Some((self.fields.offset(0), value)))
2211-
}
2212-
(&Layout::FatPointer, _) => {
2213-
Ok(Some((self.fields.offset(0) +
2214-
field.fields.offset(FAT_PTR_ADDR),
2215-
Pointer)))
2216-
}
2217-
_ => Ok(None)
2227+
let offset = self.fields.offset(0);
2228+
if let Abi::Scalar(value) = field.abi {
2229+
Ok(Some((offset, value)))
2230+
} else if let ty::TyRawPtr(_) = field.ty.sty {
2231+
// If `NonZero` contains a non-scalar `*T`, it's
2232+
// a fat pointer, which starts with a thin pointer.
2233+
Ok(Some((offset, Pointer)))
2234+
} else {
2235+
Ok(None)
22182236
}
22192237
}
22202238

22212239
// Perhaps one of the fields is non-zero, let's recurse and find out.
2222-
(&Layout::Univariant, _, _) => {
2240+
_ => {
2241+
if let FieldPlacement::Array { count, .. } = *self.fields {
2242+
if count > 0 {
2243+
return self.field(cx, 0)?.non_zero_field(cx);
2244+
}
2245+
}
22232246
for i in 0..self.fields.count() {
22242247
let r = self.field(cx, i)?.non_zero_field(cx)?;
22252248
if let Some((offset, primitive)) = r {
@@ -2228,23 +2251,6 @@ impl<'a, 'tcx> FullLayout<'tcx> {
22282251
}
22292252
Ok(None)
22302253
}
2231-
2232-
// Is this a fixed-size array of something non-zero
2233-
// with at least one element?
2234-
(_, _, &ty::TyArray(ety, _)) => {
2235-
if self.fields.count() != 0 {
2236-
cx.layout_of(ety)?.non_zero_field(cx)
2237-
} else {
2238-
Ok(None)
2239-
}
2240-
}
2241-
2242-
(_, _, &ty::TyProjection(_)) | (_, _, &ty::TyAnon(..)) => {
2243-
bug!("FullLayout::non_zero_field: {:#?} not normalized", self);
2244-
}
2245-
2246-
// Anything else is not a non-zero type.
2247-
_ => Ok(None)
22482254
}
22492255
}
22502256
}
@@ -2260,7 +2266,6 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for Layout {
22602266
Scalar => {}
22612267
Vector => {}
22622268
Array => {}
2263-
FatPointer => {}
22642269
Univariant => {}
22652270
UntaggedUnion => {}
22662271
General {

0 commit comments

Comments
 (0)