Skip to main content

rustc_ty_utils/layout/
invariant.rs

1use std::assert_matches;
2
3use rustc_abi::{BackendRepr, FieldsShape, Scalar, Size, TagEncoding, Variants};
4use rustc_middle::ty::layout::{HasTyCtxt, LayoutCx, TyAndLayout};
5use rustc_middle::{bug, ty};
6
7/// Enforce some basic invariants on layouts.
8pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
9    let tcx = cx.tcx();
10
11    if !layout.size.bytes().is_multiple_of(layout.align.bytes()) {
12        ::rustc_middle::util::bug::bug_fmt(format_args!("size is not a multiple of align, in the following layout:\n{0:#?}",
        layout));bug!("size is not a multiple of align, in the following layout:\n{layout:#?}");
13    }
14    if layout.size.bytes() >= tcx.data_layout.obj_size_bound() {
15        ::rustc_middle::util::bug::bug_fmt(format_args!("size is too large, in the following layout:\n{0:#?}",
        layout));bug!("size is too large, in the following layout:\n{layout:#?}");
16    }
17    // FIXME(#124403): Once `repr_c_enums_larger_than_int` is a hard error, we could assert
18    // here that a repr(c) enum discriminant is never larger than a c_int.
19
20    if !truecfg!(debug_assertions) {
21        // Stop here, the rest is kind of expensive.
22        return;
23    }
24
25    // Type-level uninhabitedness should always imply ABI uninhabitedness. This can be expensive on
26    // big non-exhaustive types, and is [hard to
27    // fix](https://github.com/rust-lang/rust/issues/141006#issuecomment-2883415000) in general.
28    // Only doing this sanity check when debug assertions are turned on avoids the issue for the
29    // very specific case of #140944.
30    if layout.ty.is_privately_uninhabited(tcx, cx.typing_env) {
31        if !layout.is_uninhabited() {
    {
        ::core::panicking::panic_fmt(format_args!("{0:?} is type-level uninhabited but not ABI-uninhabited?",
                layout.ty));
    }
};assert!(
32            layout.is_uninhabited(),
33            "{:?} is type-level uninhabited but not ABI-uninhabited?",
34            layout.ty
35        );
36    }
37
38    /// Yields non-ZST fields of the type
39    fn non_zst_fields<'tcx, 'a>(
40        cx: &'a LayoutCx<'tcx>,
41        layout: &'a TyAndLayout<'tcx>,
42    ) -> impl Iterator<Item = (Size, TyAndLayout<'tcx>)> {
43        (0..layout.layout.fields().count()).filter_map(|i| {
44            let field = layout.field(cx, i);
45            // Also checking `align == 1` here leads to test failures in
46            // `layout/zero-sized-array-union.rs`, where a type has a zero-size field with
47            // alignment 4 that still gets ignored during layout computation (which is okay
48            // since other fields already force alignment 4).
49            let zst = field.is_zst();
50            (!zst).then(|| (layout.fields.offset(i), field))
51        })
52    }
53
54    fn skip_newtypes<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) -> TyAndLayout<'tcx> {
55        match *layout.ty.kind() {
56            ty::UnsafeBinder(bound_ty) => {
57                let ty = cx.tcx().instantiate_bound_regions_with_erased(bound_ty.into());
58                return skip_newtypes(cx, &TyAndLayout { ty, ..*layout });
59            }
60            _ => {}
61        }
62
63        if #[allow(non_exhaustive_omitted_patterns)] match layout.layout.variants() {
    Variants::Multiple { .. } => true,
    _ => false,
}matches!(layout.layout.variants(), Variants::Multiple { .. }) {
64            // Definitely not a newtype of anything.
65            return *layout;
66        }
67        let mut fields = non_zst_fields(cx, layout);
68        let Some(first) = fields.next() else {
69            // No fields here, so this could be a primitive or enum -- either way it's not a newtype around a thing
70            return *layout;
71        };
72        if fields.next().is_none() {
73            let (offset, first) = first;
74            if offset == Size::ZERO && first.layout.size() == layout.size {
75                // This is a newtype, so keep recursing.
76                // FIXME(RalfJung): I don't think it would be correct to do any checks for
77                // alignment here, so we don't. Is that correct?
78                return skip_newtypes(cx, &first);
79            }
80        }
81        // No more newtypes here.
82        *layout
83    }
84
85    fn check_layout_abi<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayout<'tcx>) {
86        // Verify the ABI-mandated alignment and size for scalars.
87        let align = layout.backend_repr.scalar_platform_align(cx);
88        let size = layout.backend_repr.scalar_size(cx);
89        if let Some(align) = align {
90            {
    match (&layout.layout.align().abi, &align) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("alignment mismatch between ABI and layout in {0:#?}",
                            layout)));
            }
        }
    }
};assert_eq!(
91                layout.layout.align().abi,
92                align,
93                "alignment mismatch between ABI and layout in {layout:#?}"
94            );
95        }
96        if let Some(size) = size {
97            {
    match (&layout.layout.size(), &size) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("size mismatch between ABI and layout in {0:#?}",
                            layout)));
            }
        }
    }
};assert_eq!(
98                layout.layout.size(),
99                size,
100                "size mismatch between ABI and layout in {layout:#?}"
101            );
102        }
103
104        // Verify per-ABI invariants
105        match layout.layout.backend_repr() {
106            BackendRepr::Scalar(_) => {
107                // These must always be present for `Scalar` types.
108                let align = align.unwrap();
109                let size = size.unwrap();
110                // Check that this matches the underlying field.
111                let inner = skip_newtypes(cx, layout);
112                if !#[allow(non_exhaustive_omitted_patterns)] match inner.layout.backend_repr()
            {
            BackendRepr::Scalar(_) => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("`Scalar` type {0} is newtype around non-`Scalar` type {1}",
                layout.ty, inner.ty));
    }
};assert!(
113                    matches!(inner.layout.backend_repr(), BackendRepr::Scalar(_)),
114                    "`Scalar` type {} is newtype around non-`Scalar` type {}",
115                    layout.ty,
116                    inner.ty
117                );
118                match inner.layout.fields() {
119                    FieldsShape::Primitive => {
120                        // Fine.
121                    }
122                    FieldsShape::Union(..) => {
123                        // FIXME: I guess we could also check something here? Like, look at all fields?
124                        return;
125                    }
126                    FieldsShape::Arbitrary { .. } => {
127                        // Should be an enum, the only field is the discriminant.
128                        if !inner.ty.is_enum() {
    {
        ::core::panicking::panic_fmt(format_args!("`Scalar` layout for non-primitive non-enum type {0}",
                inner.ty));
    }
};assert!(
129                            inner.ty.is_enum(),
130                            "`Scalar` layout for non-primitive non-enum type {}",
131                            inner.ty
132                        );
133                        {
    match (&inner.layout.fields().count(), &1) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`Scalar` layout for multiple-field type in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
134                            inner.layout.fields().count(),
135                            1,
136                            "`Scalar` layout for multiple-field type in {inner:#?}",
137                        );
138                        let offset = inner.layout.fields().offset(0);
139                        let field = inner.field(cx, 0);
140                        // The field should be at the right offset, and match the `scalar` layout.
141                        {
    match (&offset, &Size::ZERO) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`Scalar` field at non-0 offset in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
142                            offset,
143                            Size::ZERO,
144                            "`Scalar` field at non-0 offset in {inner:#?}",
145                        );
146                        {
    match (&field.size, &size) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`Scalar` field with bad size in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(field.size, size, "`Scalar` field with bad size in {inner:#?}",);
147                        {
    match (&field.align.abi, &align) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`Scalar` field with bad align in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
148                            field.align.abi, align,
149                            "`Scalar` field with bad align in {inner:#?}",
150                        );
151                        if !#[allow(non_exhaustive_omitted_patterns)] match field.backend_repr {
            BackendRepr::Scalar(_) => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("`Scalar` field with bad ABI in {0:#?}",
                inner));
    }
};assert!(
152                            matches!(field.backend_repr, BackendRepr::Scalar(_)),
153                            "`Scalar` field with bad ABI in {inner:#?}",
154                        );
155                    }
156                    _ => {
157                        {
    ::core::panicking::panic_fmt(format_args!("`Scalar` layout for non-primitive non-enum type {0}",
            inner.ty));
};panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty);
158                    }
159                }
160            }
161            BackendRepr::ScalarPair { a: scalar1, b: scalar2, b_offset } => {
162                // Check that the underlying pair of fields matches.
163                let inner = skip_newtypes(cx, layout);
164                if !#[allow(non_exhaustive_omitted_patterns)] match inner.layout.backend_repr()
            {
            BackendRepr::ScalarPair { .. } => true,
            _ => false,
        } {
    {
        ::core::panicking::panic_fmt(format_args!("`ScalarPair` type {0} is newtype around non-`ScalarPair` type {1}",
                layout.ty, inner.ty));
    }
};assert!(
165                    matches!(inner.layout.backend_repr(), BackendRepr::ScalarPair { .. }),
166                    "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}",
167                    layout.ty,
168                    inner.ty
169                );
170                // `a` is at memory offset zero, so to keep them from overlapping the offset
171                // to `b` must be at least as much as the size of `a`.
172                if !(b_offset >= scalar1.size(cx)) {
    {
        ::core::panicking::panic_fmt(format_args!("`ScalarPair` scalars are overlapping in {0:?}",
                layout));
    }
};assert!(
173                    b_offset >= scalar1.size(cx),
174                    "`ScalarPair` scalars are overlapping in {layout:?}",
175                );
176                if #[allow(non_exhaustive_omitted_patterns)] match inner.layout.variants() {
    Variants::Multiple { .. } => true,
    _ => false,
}matches!(inner.layout.variants(), Variants::Multiple { .. }) {
177                    // FIXME: ScalarPair for enums is enormously complicated and it is very hard
178                    // to check anything about them.
179                    return;
180                }
181                match inner.layout.fields() {
182                    FieldsShape::Arbitrary { .. } => {
183                        // Checked below.
184                    }
185                    FieldsShape::Union(..) => {
186                        // FIXME: I guess we could also check something here? Like, look at all fields?
187                        return;
188                    }
189                    _ => {
190                        {
    ::core::panicking::panic_fmt(format_args!("`ScalarPair` layout with unexpected field shape in {0:#?}",
            inner));
};panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}");
191                    }
192                }
193                let mut fields = non_zst_fields(cx, &inner);
194                let (offset1, field1) = fields.next().unwrap_or_else(|| {
195                    {
    ::core::panicking::panic_fmt(format_args!("`ScalarPair` layout for type with not even one non-ZST field: {0:#?}",
            inner));
}panic!(
196                        "`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}"
197                    )
198                });
199                let (offset2, field2) = fields.next().unwrap_or_else(|| {
200                    {
    ::core::panicking::panic_fmt(format_args!("`ScalarPair` layout for type with less than two non-ZST fields: {0:#?}",
            inner));
}panic!(
201                        "`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}"
202                    )
203                });
204                {
    match fields.next() {
        None => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val, "None",
                ::core::option::Option::Some(format_args!("`ScalarPair` layout for type with at least three non-ZST fields: {0:#?}",
                        inner)));
        }
    }
};assert_matches!(
205                    fields.next(),
206                    None,
207                    "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}"
208                );
209                // The fields might be in opposite order.
210                let (offset1, field1, offset2, field2) = if offset1 <= offset2 {
211                    (offset1, field1, offset2, field2)
212                } else {
213                    (offset2, field2, offset1, field1)
214                };
215                // The fields should be at the right offset, and match the `scalar` layout.
216                let size1 = scalar1.size(cx);
217                let align1 = scalar1.default_align(cx).abi;
218                let size2 = scalar2.size(cx);
219                let align2 = scalar2.default_align(cx).abi;
220                {
    match (&offset1, &Size::ZERO) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` first field at non-0 offset in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
221                    offset1,
222                    Size::ZERO,
223                    "`ScalarPair` first field at non-0 offset in {inner:#?}",
224                );
225                {
    match (&field1.size, &size1) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` first field with bad size in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
226                    field1.size, size1,
227                    "`ScalarPair` first field with bad size in {inner:#?}",
228                );
229                {
    match (&field1.align.abi, &align1) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` first field with bad align in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
230                    field1.align.abi, align1,
231                    "`ScalarPair` first field with bad align in {inner:#?}",
232                );
233                {
    match field1.backend_repr {
        BackendRepr::Scalar(_) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::Scalar(_)",
                ::core::option::Option::Some(format_args!("`ScalarPair` first field with bad ABI in {0:#?}",
                        inner)));
        }
    }
};assert_matches!(
234                    field1.backend_repr,
235                    BackendRepr::Scalar(_),
236                    "`ScalarPair` first field with bad ABI in {inner:#?}",
237                );
238                let field2_offset = size1.align_to(align2);
239                {
    match (&offset2, &field2_offset) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` second field at bad offset in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
240                    offset2, field2_offset,
241                    "`ScalarPair` second field at bad offset in {inner:#?}",
242                );
243                {
    match (&b_offset, &field2_offset) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` with inconsistent b_offset in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
244                    b_offset, field2_offset,
245                    "`ScalarPair` with inconsistent b_offset in {inner:#?}",
246                );
247                {
    match (&field2.size, &size2) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` second field with bad size in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
248                    field2.size, size2,
249                    "`ScalarPair` second field with bad size in {inner:#?}",
250                );
251                {
    match (&field2.align.abi, &align2) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("`ScalarPair` second field with bad align in {0:#?}",
                            inner)));
            }
        }
    }
};assert_eq!(
252                    field2.align.abi, align2,
253                    "`ScalarPair` second field with bad align in {inner:#?}",
254                );
255                {
    match field2.backend_repr {
        BackendRepr::Scalar(_) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "BackendRepr::Scalar(_)",
                ::core::option::Option::Some(format_args!("`ScalarPair` second field with bad ABI in {0:#?}",
                        inner)));
        }
    }
};assert_matches!(
256                    field2.backend_repr,
257                    BackendRepr::Scalar(_),
258                    "`ScalarPair` second field with bad ABI in {inner:#?}",
259                );
260            }
261            BackendRepr::SimdVector { element, count } => {
262                let align = layout.align.abi;
263                let size = layout.size;
264                let element_align = element.default_align(cx).abi;
265                let element_size = element.size(cx);
266                // Currently, vectors must always be aligned to at least their elements:
267                if !(align >= element_align) {
    ::core::panicking::panic("assertion failed: align >= element_align")
};assert!(align >= element_align);
268                // And the size has to be element * count plus alignment padding, of course
269                if !(size == (element_size * count).align_to(align)) {
    ::core::panicking::panic("assertion failed: size == (element_size * count).align_to(align)")
};assert!(size == (element_size * count).align_to(align));
270            }
271            BackendRepr::Memory { .. } | BackendRepr::SimdScalableVector { .. } => {} // Nothing to check.
272        }
273    }
274
275    check_layout_abi(cx, layout);
276
277    match &layout.variants {
278        Variants::Empty => {
279            if !layout.is_uninhabited() {
    ::core::panicking::panic("assertion failed: layout.is_uninhabited()")
};assert!(layout.is_uninhabited());
280        }
281        Variants::Single { index } => {
282            if let Some(variants) = layout.ty.variant_range(tcx) {
283                if !variants.contains(index) {
    ::core::panicking::panic("assertion failed: variants.contains(index)")
};assert!(variants.contains(index));
284            } else {
285                // Types without variants use `0` as dummy variant index.
286                if !(index.as_u32() == 0) {
    ::core::panicking::panic("assertion failed: index.as_u32() == 0")
};assert!(index.as_u32() == 0);
287            }
288        }
289        Variants::Multiple { variants, tag, tag_encoding, .. } => {
290            if let TagEncoding::Niche { niche_start, untagged_variant, niche_variants } =
291                tag_encoding
292            {
293                let niche_size = tag.size(cx);
294                if !(*niche_start <= niche_size.unsigned_int_max()) {
    ::core::panicking::panic("assertion failed: *niche_start <= niche_size.unsigned_int_max()")
};assert!(*niche_start <= niche_size.unsigned_int_max());
295                for (idx, variant) in variants.iter_enumerated() {
296                    // Ensure all inhabited variants are accounted for.
297                    if !variant.is_uninhabited() {
298                        if !(idx == *untagged_variant || niche_variants.contains(&idx)) {
    ::core::panicking::panic("assertion failed: idx == *untagged_variant || niche_variants.contains(&idx)")
};assert!(idx == *untagged_variant || niche_variants.contains(&idx));
299                    }
300
301                    // Ensure that for niche encoded tags the discriminant coincides with the variant index.
302                    let val = layout.ty.discriminant_for_variant(tcx, idx).unwrap().val;
303                    if val != u128::from(idx.as_u32()) {
304                        let adt_def = layout.ty.ty_adt_def().unwrap();
305                        cx.tcx().dcx().span_delayed_bug(
306                            cx.tcx().def_span(adt_def.did()),
307                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("variant {0:?} has discriminant {1:?} in niche-encoded type",
                idx, val))
    })format!(
308                                "variant {idx:?} has discriminant {val:?} in niche-encoded type"
309                            ),
310                        );
311                    }
312                }
313            }
314            for variant in variants.iter() {
315                // Variants should have the same or a smaller size as the full thing.
316                if variant.size > layout.size {
317                    ::rustc_middle::util::bug::bug_fmt(format_args!("Type with size {0} bytes has variant with size {1} bytes: {2:#?}",
        layout.size.bytes(), variant.size.bytes(), layout))bug!(
318                        "Type with size {} bytes has variant with size {} bytes: {layout:#?}",
319                        layout.size.bytes(),
320                        variant.size.bytes(),
321                    )
322                }
323                // Skip empty variants.
324                if variant.size == Size::ZERO || !variant.has_fields() || variant.is_uninhabited() {
325                    // These are never actually accessed anyway, so we can skip the coherence check
326                    // for them. They also fail that check, since they may have
327                    // a different ABI even when the main type is
328                    // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size
329                    // 0, and sometimes, variants without fields have non-0 size.)
330                    continue;
331                }
332                // The top-level ABI and the ABI of the variants should be coherent.
333                let scalar_coherent = |s1: Scalar, s2: Scalar| {
334                    s1.size(cx) == s2.size(cx) && s1.default_align(cx) == s2.default_align(cx)
335                };
336                let abi_coherent = match (layout.backend_repr, variant.backend_repr) {
337                    (BackendRepr::Scalar(s1), BackendRepr::Scalar(s2)) => scalar_coherent(s1, s2),
338                    (
339                        BackendRepr::ScalarPair { a: a1, b: b1, b_offset: b1_offset },
340                        BackendRepr::ScalarPair { a: a2, b: b2, b_offset: b2_offset },
341                    ) => {
342                        scalar_coherent(a1, a2) && scalar_coherent(b1, b2) && b1_offset == b2_offset
343                    }
344                    (BackendRepr::Memory { .. }, _) => true,
345                    _ => false,
346                };
347                if !abi_coherent {
348                    ::rustc_middle::util::bug::bug_fmt(format_args!("Variant ABI is incompatible with top-level ABI:\nvariant={0:#?}\nTop-level: {1:#?}",
        variant, layout));bug!(
349                        "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}",
350                        variant
351                    );
352                }
353            }
354        }
355    }
356}