1 use rustc_middle::ty::{
2 layout::{LayoutCx, TyAndLayout},
5 use rustc_target::abi::*;
9 /// Enforce some basic invariants on layouts.
10 pub(super) fn sanity_check_layout<'tcx>(
11 cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
12 layout: &TyAndLayout<'tcx>,
14 // Type-level uninhabitedness should always imply ABI uninhabitedness.
15 if layout.ty.is_privately_uninhabited(cx.tcx, cx.param_env) {
16 assert!(layout.abi.is_uninhabited());
19 if layout.size.bytes() % layout.align.abi.bytes() != 0 {
20 bug!("size is not a multiple of align, in the following layout:\n{layout:#?}");
23 if !cfg!(debug_assertions) {
24 // Stop here, the rest is kind of expensive.
28 /// Yields non-ZST fields of the type
29 fn non_zst_fields<'tcx, 'a>(
30 cx: &'a LayoutCx<'tcx, TyCtxt<'tcx>>,
31 layout: &'a TyAndLayout<'tcx>,
32 ) -> impl Iterator<Item = (Size, TyAndLayout<'tcx>)> + 'a {
33 (0..layout.layout.fields().count()).filter_map(|i| {
34 let field = layout.field(cx, i);
35 // Also checking `align == 1` here leads to test failures in
36 // `layout/zero-sized-array-union.rs`, where a type has a zero-size field with
37 // alignment 4 that still gets ignored during layout computation (which is okay
38 // since other fields already force alignment 4).
39 let zst = field.is_zst();
40 (!zst).then(|| (layout.fields.offset(i), field))
44 fn skip_newtypes<'tcx>(
45 cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
46 layout: &TyAndLayout<'tcx>,
47 ) -> TyAndLayout<'tcx> {
48 if matches!(layout.layout.variants(), Variants::Multiple { .. }) {
49 // Definitely not a newtype of anything.
52 let mut fields = non_zst_fields(cx, layout);
53 let Some(first) = fields.next() else {
54 // No fields here, so this could be a primitive or enum -- either way it's not a newtype around a thing
57 if fields.next().is_none() {
58 let (offset, first) = first;
59 if offset == Size::ZERO && first.layout.size() == layout.size {
60 // This is a newtype, so keep recursing.
61 // FIXME(RalfJung): I don't think it would be correct to do any checks for
62 // alignment here, so we don't. Is that correct?
63 return skip_newtypes(cx, &first);
66 // No more newtypes here.
70 fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>) {
71 match layout.layout.abi() {
72 Abi::Scalar(scalar) => {
73 // No padding in scalars.
74 let size = scalar.size(cx);
75 let align = scalar.align(cx).abi;
79 "size mismatch between ABI and layout in {layout:#?}"
82 layout.layout.align().abi,
84 "alignment mismatch between ABI and layout in {layout:#?}"
86 // Check that this matches the underlying field.
87 let inner = skip_newtypes(cx, layout);
89 matches!(inner.layout.abi(), Abi::Scalar(_)),
90 "`Scalar` type {} is newtype around non-`Scalar` type {}",
94 match inner.layout.fields() {
95 FieldsShape::Primitive => {
98 FieldsShape::Union(..) => {
99 // FIXME: I guess we could also check something here? Like, look at all fields?
102 FieldsShape::Arbitrary { .. } => {
103 // Should be an enum, the only field is the discriminant.
106 "`Scalar` layout for non-primitive non-enum type {}",
110 inner.layout.fields().count(),
112 "`Scalar` layout for multiple-field type in {inner:#?}",
114 let offset = inner.layout.fields().offset(0);
115 let field = inner.field(cx, 0);
116 // The field should be at the right offset, and match the `scalar` layout.
120 "`Scalar` field at non-0 offset in {inner:#?}",
122 assert_eq!(field.size, size, "`Scalar` field with bad size in {inner:#?}",);
124 field.align.abi, align,
125 "`Scalar` field with bad align in {inner:#?}",
128 matches!(field.abi, Abi::Scalar(_)),
129 "`Scalar` field with bad ABI in {inner:#?}",
133 panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty);
137 Abi::ScalarPair(scalar1, scalar2) => {
138 // Sanity-check scalar pairs. Computing the expected size and alignment is a bit of work.
139 let size1 = scalar1.size(cx);
140 let align1 = scalar1.align(cx).abi;
141 let size2 = scalar2.size(cx);
142 let align2 = scalar2.align(cx).abi;
143 let align = cmp::max(align1, align2);
144 let field2_offset = size1.align_to(align2);
145 let size = (field2_offset + size2).align_to(align);
147 layout.layout.size(),
149 "size mismatch between ABI and layout in {layout:#?}"
152 layout.layout.align().abi,
154 "alignment mismatch between ABI and layout in {layout:#?}",
156 // Check that the underlying pair of fields matches.
157 let inner = skip_newtypes(cx, layout);
159 matches!(inner.layout.abi(), Abi::ScalarPair(..)),
160 "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}",
164 if matches!(inner.layout.variants(), Variants::Multiple { .. }) {
165 // FIXME: ScalarPair for enums is enormously complicated and it is very hard
166 // to check anything about them.
169 match inner.layout.fields() {
170 FieldsShape::Arbitrary { .. } => {
173 FieldsShape::Union(..) => {
174 // FIXME: I guess we could also check something here? Like, look at all fields?
178 panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}");
181 let mut fields = non_zst_fields(cx, &inner);
182 let (offset1, field1) = fields.next().unwrap_or_else(|| {
184 "`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}"
187 let (offset2, field2) = fields.next().unwrap_or_else(|| {
189 "`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}"
193 fields.next().is_none(),
194 "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}"
196 // The fields might be in opposite order.
197 let (offset1, field1, offset2, field2) = if offset1 <= offset2 {
198 (offset1, field1, offset2, field2)
200 (offset2, field2, offset1, field1)
202 // The fields should be at the right offset, and match the `scalar` layout.
206 "`ScalarPair` first field at non-0 offset in {inner:#?}",
210 "`ScalarPair` first field with bad size in {inner:#?}",
213 field1.align.abi, align1,
214 "`ScalarPair` first field with bad align in {inner:#?}",
217 matches!(field1.abi, Abi::Scalar(_)),
218 "`ScalarPair` first field with bad ABI in {inner:#?}",
221 offset2, field2_offset,
222 "`ScalarPair` second field at bad offset in {inner:#?}",
226 "`ScalarPair` second field with bad size in {inner:#?}",
229 field2.align.abi, align2,
230 "`ScalarPair` second field with bad align in {inner:#?}",
233 matches!(field2.abi, Abi::Scalar(_)),
234 "`ScalarPair` second field with bad ABI in {inner:#?}",
237 Abi::Vector { count, element } => {
238 // No padding in vectors, except possibly for trailing padding to make the size a multiple of align.
239 let size = element.size(cx) * count;
240 let align = cx.data_layout().vector_align(size).abi;
241 let size = size.align_to(align); // needed e.g. for vectors of size 3
242 assert!(align >= element.align(cx).abi); // just sanity-checking `vector_align`.
244 layout.layout.size(),
246 "size mismatch between ABI and layout in {layout:#?}"
249 layout.layout.align().abi,
251 "alignment mismatch between ABI and layout in {layout:#?}"
253 // FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair.
255 Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check.
259 check_layout_abi(cx, layout);
261 if let Variants::Multiple { variants, .. } = &layout.variants {
262 for variant in variants.iter() {
263 // No nested "multiple".
264 assert!(matches!(variant.variants, Variants::Single { .. }));
265 // Variants should have the same or a smaller size as the full thing,
266 // and same for alignment.
267 if variant.size > layout.size {
269 "Type with size {} bytes has variant with size {} bytes: {layout:#?}",
271 variant.size.bytes(),
274 if variant.align.abi > layout.align.abi {
276 "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}",
277 layout.align.abi.bytes(),
278 variant.align.abi.bytes(),
281 // Skip empty variants.
282 if variant.size == Size::ZERO
283 || variant.fields.count() == 0
284 || variant.abi.is_uninhabited()
286 // These are never actually accessed anyway, so we can skip the coherence check
287 // for them. They also fail that check, since they have
288 // `Aggregate`/`Uninhbaited` ABI even when the main type is
289 // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size
290 // 0, and sometimes, variants without fields have non-0 size.)
293 // The top-level ABI and the ABI of the variants should be coherent.
294 let scalar_coherent =
295 |s1: Scalar, s2: Scalar| s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx);
296 let abi_coherent = match (layout.abi, variant.abi) {
297 (Abi::Scalar(s1), Abi::Scalar(s2)) => scalar_coherent(s1, s2),
298 (Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => {
299 scalar_coherent(a1, a2) && scalar_coherent(b1, b2)
301 (Abi::Uninhabited, _) => true,
302 (Abi::Aggregate { .. }, _) => true,
307 "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}",