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