]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ty_utils/src/layout_sanity_check.rs
Auto merge of #100539 - joboet:horizon_timeout_clock, r=thomcc
[rust.git] / compiler / rustc_ty_utils / src / layout_sanity_check.rs
1 use rustc_middle::ty::{
2     layout::{LayoutCx, TyAndLayout},
3     TyCtxt,
4 };
5 use rustc_target::abi::*;
6
7 use std::cmp;
8
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>,
13 ) {
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());
17     }
18
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:#?}");
21     }
22
23     if !cfg!(debug_assertions) {
24         // Stop here, the rest is kind of expensive.
25         return;
26     }
27
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))
41         })
42     }
43
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.
50             return *layout;
51         }
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
55             return *layout
56         };
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);
64             }
65         }
66         // No more newtypes here.
67         *layout
68     }
69
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;
76                 assert_eq!(
77                     layout.layout.size(),
78                     size,
79                     "size mismatch between ABI and layout in {layout:#?}"
80                 );
81                 assert_eq!(
82                     layout.layout.align().abi,
83                     align,
84                     "alignment mismatch between ABI and layout in {layout:#?}"
85                 );
86                 // Check that this matches the underlying field.
87                 let inner = skip_newtypes(cx, layout);
88                 assert!(
89                     matches!(inner.layout.abi(), Abi::Scalar(_)),
90                     "`Scalar` type {} is newtype around non-`Scalar` type {}",
91                     layout.ty,
92                     inner.ty
93                 );
94                 match inner.layout.fields() {
95                     FieldsShape::Primitive => {
96                         // Fine.
97                     }
98                     FieldsShape::Union(..) => {
99                         // FIXME: I guess we could also check something here? Like, look at all fields?
100                         return;
101                     }
102                     FieldsShape::Arbitrary { .. } => {
103                         // Should be an enum, the only field is the discriminant.
104                         assert!(
105                             inner.ty.is_enum(),
106                             "`Scalar` layout for non-primitive non-enum type {}",
107                             inner.ty
108                         );
109                         assert_eq!(
110                             inner.layout.fields().count(),
111                             1,
112                             "`Scalar` layout for multiple-field type in {inner:#?}",
113                         );
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.
117                         assert_eq!(
118                             offset,
119                             Size::ZERO,
120                             "`Scalar` field at non-0 offset in {inner:#?}",
121                         );
122                         assert_eq!(field.size, size, "`Scalar` field with bad size in {inner:#?}",);
123                         assert_eq!(
124                             field.align.abi, align,
125                             "`Scalar` field with bad align in {inner:#?}",
126                         );
127                         assert!(
128                             matches!(field.abi, Abi::Scalar(_)),
129                             "`Scalar` field with bad ABI in {inner:#?}",
130                         );
131                     }
132                     _ => {
133                         panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty);
134                     }
135                 }
136             }
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);
146                 assert_eq!(
147                     layout.layout.size(),
148                     size,
149                     "size mismatch between ABI and layout in {layout:#?}"
150                 );
151                 assert_eq!(
152                     layout.layout.align().abi,
153                     align,
154                     "alignment mismatch between ABI and layout in {layout:#?}",
155                 );
156                 // Check that the underlying pair of fields matches.
157                 let inner = skip_newtypes(cx, layout);
158                 assert!(
159                     matches!(inner.layout.abi(), Abi::ScalarPair(..)),
160                     "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}",
161                     layout.ty,
162                     inner.ty
163                 );
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.
167                     return;
168                 }
169                 match inner.layout.fields() {
170                     FieldsShape::Arbitrary { .. } => {
171                         // Checked below.
172                     }
173                     FieldsShape::Union(..) => {
174                         // FIXME: I guess we could also check something here? Like, look at all fields?
175                         return;
176                     }
177                     _ => {
178                         panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}");
179                     }
180                 }
181                 let mut fields = non_zst_fields(cx, &inner);
182                 let (offset1, field1) = fields.next().unwrap_or_else(|| {
183                     panic!(
184                         "`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}"
185                     )
186                 });
187                 let (offset2, field2) = fields.next().unwrap_or_else(|| {
188                     panic!(
189                         "`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}"
190                     )
191                 });
192                 assert!(
193                     fields.next().is_none(),
194                     "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}"
195                 );
196                 // The fields might be in opposite order.
197                 let (offset1, field1, offset2, field2) = if offset1 <= offset2 {
198                     (offset1, field1, offset2, field2)
199                 } else {
200                     (offset2, field2, offset1, field1)
201                 };
202                 // The fields should be at the right offset, and match the `scalar` layout.
203                 assert_eq!(
204                     offset1,
205                     Size::ZERO,
206                     "`ScalarPair` first field at non-0 offset in {inner:#?}",
207                 );
208                 assert_eq!(
209                     field1.size, size1,
210                     "`ScalarPair` first field with bad size in {inner:#?}",
211                 );
212                 assert_eq!(
213                     field1.align.abi, align1,
214                     "`ScalarPair` first field with bad align in {inner:#?}",
215                 );
216                 assert!(
217                     matches!(field1.abi, Abi::Scalar(_)),
218                     "`ScalarPair` first field with bad ABI in {inner:#?}",
219                 );
220                 assert_eq!(
221                     offset2, field2_offset,
222                     "`ScalarPair` second field at bad offset in {inner:#?}",
223                 );
224                 assert_eq!(
225                     field2.size, size2,
226                     "`ScalarPair` second field with bad size in {inner:#?}",
227                 );
228                 assert_eq!(
229                     field2.align.abi, align2,
230                     "`ScalarPair` second field with bad align in {inner:#?}",
231                 );
232                 assert!(
233                     matches!(field2.abi, Abi::Scalar(_)),
234                     "`ScalarPair` second field with bad ABI in {inner:#?}",
235                 );
236             }
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`.
243                 assert_eq!(
244                     layout.layout.size(),
245                     size,
246                     "size mismatch between ABI and layout in {layout:#?}"
247                 );
248                 assert_eq!(
249                     layout.layout.align().abi,
250                     align,
251                     "alignment mismatch between ABI and layout in {layout:#?}"
252                 );
253                 // FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair.
254             }
255             Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check.
256         }
257     }
258
259     check_layout_abi(cx, layout);
260
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 {
268                 bug!(
269                     "Type with size {} bytes has variant with size {} bytes: {layout:#?}",
270                     layout.size.bytes(),
271                     variant.size.bytes(),
272                 )
273             }
274             if variant.align.abi > layout.align.abi {
275                 bug!(
276                     "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}",
277                     layout.align.abi.bytes(),
278                     variant.align.abi.bytes(),
279                 )
280             }
281             // Skip empty variants.
282             if variant.size == Size::ZERO
283                 || variant.fields.count() == 0
284                 || variant.abi.is_uninhabited()
285             {
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.)
291                 continue;
292             }
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)
300                 }
301                 (Abi::Uninhabited, _) => true,
302                 (Abi::Aggregate { .. }, _) => true,
303                 _ => false,
304             };
305             if !abi_coherent {
306                 bug!(
307                     "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}",
308                     variant
309                 );
310             }
311         }
312     }
313 }