@@ -135,22 +135,24 @@ pub(super) fn sanity_check_layout<'tcx>(
135
135
}
136
136
}
137
137
Abi :: ScalarPair ( scalar1, scalar2) => {
138
- // Sanity-check scalar pairs. These are a bit more flexible and support
139
- // padding, but we can at least ensure both fields actually fit into the layout
140
- // and the alignment requirement has not been weakened.
138
+ // Sanity-check scalar pairs. Computing the expected size and alignment is a bit of work.
141
139
let size1 = scalar1. size ( cx) ;
142
140
let align1 = scalar1. align ( cx) . abi ;
143
141
let size2 = scalar2. size ( cx) ;
144
142
let align2 = scalar2. align ( cx) . abi ;
145
- assert ! (
146
- layout. layout. align( ) . abi >= cmp:: max( align1, align2) ,
147
- "alignment mismatch between ABI and layout in {layout:#?}" ,
148
- ) ;
143
+ let align = cmp:: max ( align1, align2) ;
149
144
let field2_offset = size1. align_to ( align2) ;
150
- assert ! (
151
- layout. layout. size( ) >= field2_offset + size2,
145
+ let size = ( field2_offset + size2) . align_to ( align) ;
146
+ assert_eq ! (
147
+ layout. layout. size( ) ,
148
+ size,
152
149
"size mismatch between ABI and layout in {layout:#?}"
153
150
) ;
151
+ assert_eq ! (
152
+ layout. layout. align( ) . abi,
153
+ align,
154
+ "alignment mismatch between ABI and layout in {layout:#?}" ,
155
+ ) ;
154
156
// Check that the underlying pair of fields matches.
155
157
let inner = skip_newtypes ( cx, layout) ;
156
158
assert ! (
@@ -233,17 +235,22 @@ pub(super) fn sanity_check_layout<'tcx>(
233
235
) ;
234
236
}
235
237
Abi :: Vector { count, element } => {
236
- // No padding in vectors. Alignment can be strengthened, though.
237
- assert ! (
238
- layout. layout. align( ) . abi >= element. align( cx) . abi,
239
- "alignment mismatch between ABI and layout in {layout:#?}"
240
- ) ;
238
+ // No padding in vectors, except possibly for trailing padding to make the size a multiple of align.
241
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`.
242
243
assert_eq ! (
243
244
layout. layout. size( ) ,
244
- size. align_to ( cx . data_layout ( ) . vector_align ( size ) . abi ) ,
245
+ size,
245
246
"size mismatch between ABI and layout in {layout:#?}"
246
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.
247
254
}
248
255
Abi :: Uninhabited | Abi :: Aggregate { .. } => { } // Nothing to check.
249
256
}
0 commit comments