@@ -102,41 +102,27 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
102
102
Self { cx }
103
103
}
104
104
105
- pub fn scalar_pair < FieldIdx : Idx , VariantIdx : Idx > (
105
+ pub fn array < FieldIdx : Idx , VariantIdx : Idx , F > (
106
106
& self ,
107
- a : Scalar ,
108
- b : Scalar ,
109
- ) -> LayoutData < FieldIdx , VariantIdx > {
110
- let dl = self . cx . data_layout ( ) ;
111
- let b_align = b. align ( dl) ;
112
- let align = a. align ( dl) . max ( b_align) . max ( dl. aggregate_align ) ;
113
- let b_offset = a. size ( dl) . align_to ( b_align. abi ) ;
114
- let size = ( b_offset + b. size ( dl) ) . align_to ( align. abi ) ;
115
-
116
- // HACK(nox): We iter on `b` and then `a` because `max_by_key`
117
- // returns the last maximum.
118
- let largest_niche = Niche :: from_scalar ( dl, b_offset, b)
119
- . into_iter ( )
120
- . chain ( Niche :: from_scalar ( dl, Size :: ZERO , a) )
121
- . max_by_key ( |niche| niche. available ( dl) ) ;
122
-
123
- let combined_seed = a. size ( & self . cx ) . bytes ( ) . wrapping_add ( b. size ( & self . cx ) . bytes ( ) ) ;
107
+ element : & LayoutData < FieldIdx , VariantIdx > ,
108
+ count_if_sized : Option < u64 > , // None for slices
109
+ ) -> LayoutCalculatorResult < FieldIdx , VariantIdx , F > {
110
+ let count = count_if_sized. unwrap_or ( 0 ) ;
111
+ let size =
112
+ element. size . checked_mul ( count, & self . cx ) . ok_or ( LayoutCalculatorError :: SizeOverflow ) ?;
124
113
125
- LayoutData {
114
+ Ok ( LayoutData {
126
115
variants : Variants :: Single { index : VariantIdx :: new ( 0 ) } ,
127
- fields : FieldsShape :: Arbitrary {
128
- offsets : [ Size :: ZERO , b_offset] . into ( ) ,
129
- memory_index : [ 0 , 1 ] . into ( ) ,
130
- } ,
131
- backend_repr : BackendRepr :: ScalarPair ( a, b) ,
132
- largest_niche,
133
- uninhabited : false ,
134
- align,
116
+ fields : FieldsShape :: Array { stride : element. size , count } ,
117
+ backend_repr : BackendRepr :: Memory { sized : count_if_sized. is_some ( ) } ,
118
+ largest_niche : element. largest_niche . filter ( |_| count != 0 ) ,
119
+ uninhabited : element. uninhabited && count != 0 ,
120
+ align : element. align ,
135
121
size,
136
122
max_repr_align : None ,
137
- unadjusted_abi_align : align. abi ,
138
- randomization_seed : Hash64 :: new ( combined_seed ) ,
139
- }
123
+ unadjusted_abi_align : element . align . abi ,
124
+ randomization_seed : element . randomization_seed . wrapping_add ( Hash64 :: new ( count ) ) ,
125
+ } )
140
126
}
141
127
142
128
pub fn univariant <
@@ -214,25 +200,6 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
214
200
layout
215
201
}
216
202
217
- pub fn layout_of_never_type < FieldIdx : Idx , VariantIdx : Idx > (
218
- & self ,
219
- ) -> LayoutData < FieldIdx , VariantIdx > {
220
- let dl = self . cx . data_layout ( ) ;
221
- // This is also used for uninhabited enums, so we use `Variants::Empty`.
222
- LayoutData {
223
- variants : Variants :: Empty ,
224
- fields : FieldsShape :: Primitive ,
225
- backend_repr : BackendRepr :: Memory { sized : true } ,
226
- largest_niche : None ,
227
- uninhabited : true ,
228
- align : dl. i8_align ,
229
- size : Size :: ZERO ,
230
- max_repr_align : None ,
231
- unadjusted_abi_align : dl. i8_align . abi ,
232
- randomization_seed : Hash64 :: ZERO ,
233
- }
234
- }
235
-
236
203
pub fn layout_of_struct_or_enum <
237
204
' a ,
238
205
FieldIdx : Idx ,
@@ -260,7 +227,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
260
227
Some ( present_first) => present_first,
261
228
// Uninhabited because it has no variants, or only absent ones.
262
229
None if is_enum => {
263
- return Ok ( self . layout_of_never_type ( ) ) ;
230
+ return Ok ( LayoutData :: never_type ( & self . cx ) ) ;
264
231
}
265
232
// If it's a struct, still compute a layout so that we can still compute the
266
233
// field offsets.
@@ -949,7 +916,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
949
916
// Common prim might be uninit.
950
917
Scalar :: Union { value : prim }
951
918
} ;
952
- let pair = self . scalar_pair :: < FieldIdx , VariantIdx > ( tag, prim_scalar) ;
919
+ let pair =
920
+ LayoutData :: < FieldIdx , VariantIdx > :: scalar_pair ( & self . cx , tag, prim_scalar) ;
953
921
let pair_offsets = match pair. fields {
954
922
FieldsShape :: Arbitrary { ref offsets, ref memory_index } => {
955
923
assert_eq ! ( memory_index. raw, [ 0 , 1 ] ) ;
@@ -1341,7 +1309,8 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
1341
1309
} else {
1342
1310
( ( j, b) , ( i, a) )
1343
1311
} ;
1344
- let pair = self . scalar_pair :: < FieldIdx , VariantIdx > ( a, b) ;
1312
+ let pair =
1313
+ LayoutData :: < FieldIdx , VariantIdx > :: scalar_pair ( & self . cx , a, b) ;
1345
1314
let pair_offsets = match pair. fields {
1346
1315
FieldsShape :: Arbitrary { ref offsets, ref memory_index } => {
1347
1316
assert_eq ! ( memory_index. raw, [ 0 , 1 ] ) ;
0 commit comments