@@ -6,24 +6,22 @@ use base_db::ra_salsa::Cycle;
6
6
use chalk_ir:: { AdtId , FloatTy , IntTy , TyKind , UintTy } ;
7
7
use hir_def:: {
8
8
layout:: {
9
- BackendRepr , FieldsShape , Float , Integer , LayoutCalculator , LayoutCalculatorError ,
10
- LayoutData , Primitive , ReprOptions , Scalar , Size , StructKind , TargetDataLayout ,
9
+ Float , Integer , LayoutCalculator , LayoutCalculatorError ,
10
+ LayoutData , Primitive , ReprOptions , Scalar , StructKind , TargetDataLayout ,
11
11
WrappingRange ,
12
12
} ,
13
13
LocalFieldId , StructId ,
14
14
} ;
15
15
use la_arena:: { Idx , RawIdx } ;
16
16
use rustc_abi:: AddressSpace ;
17
- use rustc_hashes:: Hash64 ;
18
- use rustc_index:: { IndexSlice , IndexVec } ;
17
+ use rustc_index:: IndexVec ;
19
18
20
19
use triomphe:: Arc ;
21
20
22
21
use crate :: {
23
22
consteval:: try_const_usize,
24
23
db:: { HirDatabase , InternedClosure } ,
25
24
infer:: normalize,
26
- layout:: adt:: struct_variant_idx,
27
25
utils:: ClosureSubst ,
28
26
Interner , ProjectionTy , Substitution , TraitEnvironment , Ty ,
29
27
} ;
@@ -125,10 +123,10 @@ impl<'a> LayoutCx<'a> {
125
123
}
126
124
}
127
125
128
- // FIXME: move this to the `rustc_abi`.
129
126
fn layout_of_simd_ty (
130
127
db : & dyn HirDatabase ,
131
128
id : StructId ,
129
+ repr_packed : bool ,
132
130
subst : & Substitution ,
133
131
env : Arc < TraitEnvironment > ,
134
132
dl : & TargetDataLayout ,
@@ -149,33 +147,10 @@ fn layout_of_simd_ty(
149
147
} ;
150
148
151
149
let e_len = try_const_usize ( db, & e_len) . ok_or ( LayoutError :: HasErrorConst ) ? as u64 ;
152
-
153
- // Compute the ABI of the element type:
154
150
let e_ly = db. layout_of_ty ( e_ty, env) ?;
155
- let BackendRepr :: Scalar ( e_abi) = e_ly. backend_repr else {
156
- return Err ( LayoutError :: Unknown ) ;
157
- } ;
158
151
159
- // Compute the size and alignment of the vector:
160
- let size = e_ly
161
- . size
162
- . checked_mul ( e_len, dl)
163
- . ok_or ( LayoutError :: BadCalc ( LayoutCalculatorError :: SizeOverflow ) ) ?;
164
- let align = dl. llvmlike_vector_align ( size) ;
165
- let size = size. align_to ( align. abi ) ;
166
-
167
- Ok ( Arc :: new ( Layout {
168
- variants : Variants :: Single { index : struct_variant_idx ( ) } ,
169
- fields : FieldsShape :: Arbitrary { offsets : [ Size :: ZERO ] . into ( ) , memory_index : [ 0 ] . into ( ) } ,
170
- backend_repr : BackendRepr :: SimdVector { element : e_abi, count : e_len } ,
171
- largest_niche : e_ly. largest_niche ,
172
- uninhabited : false ,
173
- size,
174
- align,
175
- max_repr_align : None ,
176
- unadjusted_abi_align : align. abi ,
177
- randomization_seed : Hash64 :: ZERO ,
178
- } ) )
152
+ let cx = LayoutCx :: new ( dl) ;
153
+ Ok ( Arc :: new ( cx. calc . simd_type ( e_ly, e_len, repr_packed) ?) )
179
154
}
180
155
181
156
pub fn layout_of_ty_query (
@@ -190,13 +165,14 @@ pub fn layout_of_ty_query(
190
165
let dl = & * target;
191
166
let cx = LayoutCx :: new ( dl) ;
192
167
let ty = normalize ( db, trait_env. clone ( ) , ty) ;
193
- let result = match ty. kind ( Interner ) {
168
+ let kind = ty. kind ( Interner ) ;
169
+ let result = match kind {
194
170
TyKind :: Adt ( AdtId ( def) , subst) => {
195
171
if let hir_def:: AdtId :: StructId ( s) = def {
196
172
let data = db. struct_data ( * s) ;
197
173
let repr = data. repr . unwrap_or_default ( ) ;
198
174
if repr. simd ( ) {
199
- return layout_of_simd_ty ( db, * s, subst, trait_env, & target) ;
175
+ return layout_of_simd_ty ( db, * s, repr . packed ( ) , subst, trait_env, & target) ;
200
176
}
201
177
} ;
202
178
return db. layout_of_adt ( * def, subst. clone ( ) , trait_env) ;
@@ -216,7 +192,7 @@ pub fn layout_of_ty_query(
216
192
valid_range : WrappingRange { start : 0 , end : 0x10FFFF } ,
217
193
} ,
218
194
) ,
219
- chalk_ir:: Scalar :: Int ( i) => scalar (
195
+ chalk_ir:: Scalar :: Int ( i) => Layout :: scalar ( dl , scalar_unit (
220
196
dl,
221
197
Primitive :: Int (
222
198
match i {
@@ -229,8 +205,8 @@ pub fn layout_of_ty_query(
229
205
} ,
230
206
true ,
231
207
) ,
232
- ) ,
233
- chalk_ir:: Scalar :: Uint ( i) => scalar (
208
+ ) ) ,
209
+ chalk_ir:: Scalar :: Uint ( i) => Layout :: scalar ( dl , scalar_unit (
234
210
dl,
235
211
Primitive :: Int (
236
212
match i {
@@ -243,16 +219,16 @@ pub fn layout_of_ty_query(
243
219
} ,
244
220
false ,
245
221
) ,
246
- ) ,
247
- chalk_ir:: Scalar :: Float ( f) => scalar (
222
+ ) ) ,
223
+ chalk_ir:: Scalar :: Float ( f) => Layout :: scalar ( dl , scalar_unit (
248
224
dl,
249
225
Primitive :: Float ( match f {
250
226
FloatTy :: F16 => Float :: F16 ,
251
227
FloatTy :: F32 => Float :: F32 ,
252
228
FloatTy :: F64 => Float :: F64 ,
253
229
FloatTy :: F128 => Float :: F128 ,
254
230
} ) ,
255
- ) ,
231
+ ) ) ,
256
232
} ,
257
233
TyKind :: Tuple ( len, tys) => {
258
234
let kind = if * len == 0 { StructKind :: AlwaysSized } else { StructKind :: MaybeUnsized } ;
@@ -268,56 +244,16 @@ pub fn layout_of_ty_query(
268
244
TyKind :: Array ( element, count) => {
269
245
let count = try_const_usize ( db, count) . ok_or ( LayoutError :: HasErrorConst ) ? as u64 ;
270
246
let element = db. layout_of_ty ( element. clone ( ) , trait_env) ?;
271
- let size = element
272
- . size
273
- . checked_mul ( count, dl)
274
- . ok_or ( LayoutError :: BadCalc ( LayoutCalculatorError :: SizeOverflow ) ) ?;
275
-
276
- let backend_repr = BackendRepr :: Memory { sized : true } ;
277
-
278
- let largest_niche = if count != 0 { element. largest_niche } else { None } ;
279
- let uninhabited = if count != 0 { element. uninhabited } else { false } ;
280
-
281
- Layout {
282
- variants : Variants :: Single { index : struct_variant_idx ( ) } ,
283
- fields : FieldsShape :: Array { stride : element. size , count } ,
284
- backend_repr,
285
- largest_niche,
286
- uninhabited,
287
- align : element. align ,
288
- size,
289
- max_repr_align : None ,
290
- unadjusted_abi_align : element. align . abi ,
291
- randomization_seed : Hash64 :: ZERO ,
292
- }
247
+ cx. calc . array_like :: < _ , _ , ( ) > ( & element, Some ( count) ) ?
293
248
}
294
249
TyKind :: Slice ( element) => {
295
250
let element = db. layout_of_ty ( element. clone ( ) , trait_env) ?;
296
- Layout {
297
- variants : Variants :: Single { index : struct_variant_idx ( ) } ,
298
- fields : FieldsShape :: Array { stride : element. size , count : 0 } ,
299
- backend_repr : BackendRepr :: Memory { sized : false } ,
300
- largest_niche : None ,
301
- uninhabited : false ,
302
- align : element. align ,
303
- size : Size :: ZERO ,
304
- max_repr_align : None ,
305
- unadjusted_abi_align : element. align . abi ,
306
- randomization_seed : Hash64 :: ZERO ,
307
- }
251
+ cx. calc . array_like :: < _ , _ , ( ) > ( & element, None ) ?
252
+ }
253
+ TyKind :: Str => {
254
+ let element = scalar_unit ( dl, Primitive :: Int ( Integer :: I8 , false ) ) ;
255
+ cx. calc . array_like :: < _ , _ , ( ) > ( & Layout :: scalar ( dl, element) , None ) ?
308
256
}
309
- TyKind :: Str => Layout {
310
- variants : Variants :: Single { index : struct_variant_idx ( ) } ,
311
- fields : FieldsShape :: Array { stride : Size :: from_bytes ( 1 ) , count : 0 } ,
312
- backend_repr : BackendRepr :: Memory { sized : false } ,
313
- largest_niche : None ,
314
- uninhabited : false ,
315
- align : dl. i8_align ,
316
- size : Size :: ZERO ,
317
- max_repr_align : None ,
318
- unadjusted_abi_align : dl. i8_align . abi ,
319
- randomization_seed : Hash64 :: ZERO ,
320
- } ,
321
257
// Potentially-wide pointers.
322
258
TyKind :: Ref ( _, _, pointee) | TyKind :: Raw ( _, pointee) => {
323
259
let mut data_ptr = scalar_unit ( dl, Primitive :: Pointer ( AddressSpace :: DATA ) ) ;
@@ -355,17 +291,12 @@ pub fn layout_of_ty_query(
355
291
} ;
356
292
357
293
// Effectively a (ptr, meta) tuple.
358
- cx . calc . scalar_pair ( data_ptr, metadata)
294
+ LayoutData :: scalar_pair ( dl , data_ptr, metadata)
359
295
}
360
- TyKind :: FnDef ( _, _) => layout_of_unit ( & cx) ?,
361
- TyKind :: Never => cx. calc . layout_of_never_type ( ) ,
362
- TyKind :: Dyn ( _) | TyKind :: Foreign ( _) => {
363
- let mut unit = layout_of_unit ( & cx) ?;
364
- match & mut unit. backend_repr {
365
- BackendRepr :: Memory { sized } => * sized = false ,
366
- _ => return Err ( LayoutError :: Unknown ) ,
367
- }
368
- unit
296
+ TyKind :: Never => LayoutData :: never_type ( dl) ,
297
+ TyKind :: FnDef ( ..) | TyKind :: Dyn ( _) | TyKind :: Foreign ( _) => {
298
+ let sized = matches ! ( kind, TyKind :: FnDef ( ..) ) ;
299
+ LayoutData :: unit ( dl, sized)
369
300
}
370
301
TyKind :: Function ( _) => {
371
302
let mut ptr = scalar_unit ( dl, Primitive :: Pointer ( dl. instruction_address_space ) ) ;
@@ -434,16 +365,6 @@ pub fn layout_of_ty_recover(
434
365
Err ( LayoutError :: RecursiveTypeWithoutIndirection )
435
366
}
436
367
437
- fn layout_of_unit ( cx : & LayoutCx < ' _ > ) -> Result < Layout , LayoutError > {
438
- cx. calc
439
- . univariant :: < RustcFieldIdx , RustcEnumVariantIdx , & & Layout > (
440
- IndexSlice :: empty ( ) ,
441
- & ReprOptions :: default ( ) ,
442
- StructKind :: AlwaysSized ,
443
- )
444
- . map_err ( Into :: into)
445
- }
446
-
447
368
fn struct_tail_erasing_lifetimes ( db : & dyn HirDatabase , pointee : Ty ) -> Ty {
448
369
match pointee. kind ( Interner ) {
449
370
TyKind :: Adt ( AdtId ( hir_def:: AdtId :: StructId ( i) ) , subst) => {
@@ -474,9 +395,5 @@ fn scalar_unit(dl: &TargetDataLayout, value: Primitive) -> Scalar {
474
395
Scalar :: Initialized { value, valid_range : WrappingRange :: full ( value. size ( dl) ) }
475
396
}
476
397
477
- fn scalar ( dl : & TargetDataLayout , value : Primitive ) -> Layout {
478
- Layout :: scalar ( dl, scalar_unit ( dl, value) )
479
- }
480
-
481
398
#[ cfg( test) ]
482
399
mod tests;
0 commit comments