1 use rustc_middle::ty::{
2 layout::{LayoutCx, TyAndLayout},
3 TyCtxt,
4 };
5 use rustc_target::abi::*;
6
7 use std::assert_matches::assert_matches;
8
9 /// Enforce some basic invariants on layouts.
sanity_check_layout<'tcx>( cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, layout: &TyAndLayout<'tcx>, )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 // Verify the ABI mandated alignment and size.
72 let align = layout.abi.inherent_align(cx).map(|align| align.abi);
73 let size = layout.abi.inherent_size(cx);
74 let Some((align, size)) = align.zip(size) else {
75 assert_matches!(
76 layout.layout.abi(),
77 Abi::Uninhabited | Abi::Aggregate { .. },
78 "ABI unexpectedly missing alignment and/or size in {layout:#?}"
79 );
80 return
81 };
82 assert_eq!(
83 layout.layout.align().abi,
84 align,
85 "alignment mismatch between ABI and layout in {layout:#?}"
86 );
87 assert_eq!(
88 layout.layout.size(),
89 size,
90 "size mismatch between ABI and layout in {layout:#?}"
91 );
92
93 // Verify per-ABI invariants
94 match layout.layout.abi() {
95 Abi::Scalar(_) => {
96 // Check that this matches the underlying field.
97 let inner = skip_newtypes(cx, layout);
98 assert!(
99 matches!(inner.layout.abi(), Abi::Scalar(_)),
100 "`Scalar` type {} is newtype around non-`Scalar` type {}",
101 layout.ty,
102 inner.ty
103 );
104 match inner.layout.fields() {
105 FieldsShape::Primitive => {
106 // Fine.
107 }
108 FieldsShape::Union(..) => {
109 // FIXME: I guess we could also check something here? Like, look at all fields?
110 return;
111 }
112 FieldsShape::Arbitrary { .. } => {
113 // Should be an enum, the only field is the discriminant.
114 assert!(
115 inner.ty.is_enum(),
116 "`Scalar` layout for non-primitive non-enum type {}",
117 inner.ty
118 );
119 assert_eq!(
120 inner.layout.fields().count(),
121 1,
122 "`Scalar` layout for multiple-field type in {inner:#?}",
123 );
124 let offset = inner.layout.fields().offset(0);
125 let field = inner.field(cx, 0);
126 // The field should be at the right offset, and match the `scalar` layout.
127 assert_eq!(
128 offset,
129 Size::ZERO,
130 "`Scalar` field at non-0 offset in {inner:#?}",
131 );
132 assert_eq!(field.size, size, "`Scalar` field with bad size in {inner:#?}",);
133 assert_eq!(
134 field.align.abi, align,
135 "`Scalar` field with bad align in {inner:#?}",
136 );
137 assert!(
138 matches!(field.abi, Abi::Scalar(_)),
139 "`Scalar` field with bad ABI in {inner:#?}",
140 );
141 }
142 _ => {
143 panic!("`Scalar` layout for non-primitive non-enum type {}", inner.ty);
144 }
145 }
146 }
147 Abi::ScalarPair(scalar1, scalar2) => {
148 // Check that the underlying pair of fields matches.
149 let inner = skip_newtypes(cx, layout);
150 assert!(
151 matches!(inner.layout.abi(), Abi::ScalarPair(..)),
152 "`ScalarPair` type {} is newtype around non-`ScalarPair` type {}",
153 layout.ty,
154 inner.ty
155 );
156 if matches!(inner.layout.variants(), Variants::Multiple { .. }) {
157 // FIXME: ScalarPair for enums is enormously complicated and it is very hard
158 // to check anything about them.
159 return;
160 }
161 match inner.layout.fields() {
162 FieldsShape::Arbitrary { .. } => {
163 // Checked below.
164 }
165 FieldsShape::Union(..) => {
166 // FIXME: I guess we could also check something here? Like, look at all fields?
167 return;
168 }
169 _ => {
170 panic!("`ScalarPair` layout with unexpected field shape in {inner:#?}");
171 }
172 }
173 let mut fields = non_zst_fields(cx, &inner);
174 let (offset1, field1) = fields.next().unwrap_or_else(|| {
175 panic!(
176 "`ScalarPair` layout for type with not even one non-ZST field: {inner:#?}"
177 )
178 });
179 let (offset2, field2) = fields.next().unwrap_or_else(|| {
180 panic!(
181 "`ScalarPair` layout for type with less than two non-ZST fields: {inner:#?}"
182 )
183 });
184 assert_matches!(
185 fields.next(),
186 None,
187 "`ScalarPair` layout for type with at least three non-ZST fields: {inner:#?}"
188 );
189 // The fields might be in opposite order.
190 let (offset1, field1, offset2, field2) = if offset1 <= offset2 {
191 (offset1, field1, offset2, field2)
192 } else {
193 (offset2, field2, offset1, field1)
194 };
195 // The fields should be at the right offset, and match the `scalar` layout.
196 let size1 = scalar1.size(cx);
197 let align1 = scalar1.align(cx).abi;
198 let size2 = scalar2.size(cx);
199 let align2 = scalar2.align(cx).abi;
200 assert_eq!(
201 offset1,
202 Size::ZERO,
203 "`ScalarPair` first field at non-0 offset in {inner:#?}",
204 );
205 assert_eq!(
206 field1.size, size1,
207 "`ScalarPair` first field with bad size in {inner:#?}",
208 );
209 assert_eq!(
210 field1.align.abi, align1,
211 "`ScalarPair` first field with bad align in {inner:#?}",
212 );
213 assert_matches!(
214 field1.abi,
215 Abi::Scalar(_),
216 "`ScalarPair` first field with bad ABI in {inner:#?}",
217 );
218 let field2_offset = size1.align_to(align2);
219 assert_eq!(
220 offset2, field2_offset,
221 "`ScalarPair` second field at bad offset in {inner:#?}",
222 );
223 assert_eq!(
224 field2.size, size2,
225 "`ScalarPair` second field with bad size in {inner:#?}",
226 );
227 assert_eq!(
228 field2.align.abi, align2,
229 "`ScalarPair` second field with bad align in {inner:#?}",
230 );
231 assert_matches!(
232 field2.abi,
233 Abi::Scalar(_),
234 "`ScalarPair` second field with bad ABI in {inner:#?}",
235 );
236 }
237 Abi::Vector { element, .. } => {
238 assert!(align >= element.align(cx).abi); // just sanity-checking `vector_align`.
239 // FIXME: Do some kind of check of the inner type, like for Scalar and ScalarPair.
240 }
241 Abi::Uninhabited | Abi::Aggregate { .. } => {} // Nothing to check.
242 }
243 }
244
245 check_layout_abi(cx, layout);
246
247 if let Variants::Multiple { variants, .. } = &layout.variants {
248 for variant in variants.iter() {
249 // No nested "multiple".
250 assert!(matches!(variant.variants, Variants::Single { .. }));
251 // Variants should have the same or a smaller size as the full thing,
252 // and same for alignment.
253 if variant.size > layout.size {
254 bug!(
255 "Type with size {} bytes has variant with size {} bytes: {layout:#?}",
256 layout.size.bytes(),
257 variant.size.bytes(),
258 )
259 }
260 if variant.align.abi > layout.align.abi {
261 bug!(
262 "Type with alignment {} bytes has variant with alignment {} bytes: {layout:#?}",
263 layout.align.abi.bytes(),
264 variant.align.abi.bytes(),
265 )
266 }
267 // Skip empty variants.
268 if variant.size == Size::ZERO
269 || variant.fields.count() == 0
270 || variant.abi.is_uninhabited()
271 {
272 // These are never actually accessed anyway, so we can skip the coherence check
273 // for them. They also fail that check, since they have
274 // `Aggregate`/`Uninhabited` ABI even when the main type is
275 // `Scalar`/`ScalarPair`. (Note that sometimes, variants with fields have size
276 // 0, and sometimes, variants without fields have non-0 size.)
277 continue;
278 }
279 // The top-level ABI and the ABI of the variants should be coherent.
280 let scalar_coherent =
281 |s1: Scalar, s2: Scalar| s1.size(cx) == s2.size(cx) && s1.align(cx) == s2.align(cx);
282 let abi_coherent = match (layout.abi, variant.abi) {
283 (Abi::Scalar(s1), Abi::Scalar(s2)) => scalar_coherent(s1, s2),
284 (Abi::ScalarPair(a1, b1), Abi::ScalarPair(a2, b2)) => {
285 scalar_coherent(a1, a2) && scalar_coherent(b1, b2)
286 }
287 (Abi::Uninhabited, _) => true,
288 (Abi::Aggregate { .. }, _) => true,
289 _ => false,
290 };
291 if !abi_coherent {
292 bug!(
293 "Variant ABI is incompatible with top-level ABI:\nvariant={:#?}\nTop-level: {layout:#?}",
294 variant
295 );
296 }
297 }
298 }
299 }
300