@@ -15,7 +15,7 @@ use hir_def::{
15
15
use la_arena:: { Idx , RawIdx } ;
16
16
use rustc_abi:: AddressSpace ;
17
17
use rustc_hashes:: Hash64 ;
18
- use rustc_index:: { IndexSlice , IndexVec } ;
18
+ use rustc_index:: IndexVec ;
19
19
20
20
use triomphe:: Arc ;
21
21
@@ -190,7 +190,8 @@ pub fn layout_of_ty_query(
190
190
let dl = & * target;
191
191
let cx = LayoutCx :: new ( dl) ;
192
192
let ty = normalize ( db, trait_env. clone ( ) , ty) ;
193
- let result = match ty. kind ( Interner ) {
193
+ let kind = ty. kind ( Interner ) ;
194
+ let result = match kind {
194
195
TyKind :: Adt ( AdtId ( def) , subst) => {
195
196
if let hir_def:: AdtId :: StructId ( s) = def {
196
197
let data = db. struct_data ( * s) ;
@@ -216,7 +217,7 @@ pub fn layout_of_ty_query(
216
217
valid_range : WrappingRange { start : 0 , end : 0x10FFFF } ,
217
218
} ,
218
219
) ,
219
- chalk_ir:: Scalar :: Int ( i) => scalar (
220
+ chalk_ir:: Scalar :: Int ( i) => Layout :: scalar ( dl , scalar_unit (
220
221
dl,
221
222
Primitive :: Int (
222
223
match i {
@@ -229,8 +230,8 @@ pub fn layout_of_ty_query(
229
230
} ,
230
231
true ,
231
232
) ,
232
- ) ,
233
- chalk_ir:: Scalar :: Uint ( i) => scalar (
233
+ ) ) ,
234
+ chalk_ir:: Scalar :: Uint ( i) => Layout :: scalar ( dl , scalar_unit (
234
235
dl,
235
236
Primitive :: Int (
236
237
match i {
@@ -243,16 +244,16 @@ pub fn layout_of_ty_query(
243
244
} ,
244
245
false ,
245
246
) ,
246
- ) ,
247
- chalk_ir:: Scalar :: Float ( f) => scalar (
247
+ ) ) ,
248
+ chalk_ir:: Scalar :: Float ( f) => Layout :: scalar ( dl , scalar_unit (
248
249
dl,
249
250
Primitive :: Float ( match f {
250
251
FloatTy :: F16 => Float :: F16 ,
251
252
FloatTy :: F32 => Float :: F32 ,
252
253
FloatTy :: F64 => Float :: F64 ,
253
254
FloatTy :: F128 => Float :: F128 ,
254
255
} ) ,
255
- ) ,
256
+ ) ) ,
256
257
} ,
257
258
TyKind :: Tuple ( len, tys) => {
258
259
let kind = if * len == 0 { StructKind :: AlwaysSized } else { StructKind :: MaybeUnsized } ;
@@ -268,56 +269,16 @@ pub fn layout_of_ty_query(
268
269
TyKind :: Array ( element, count) => {
269
270
let count = try_const_usize ( db, count) . ok_or ( LayoutError :: HasErrorConst ) ? as u64 ;
270
271
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
- }
272
+ cx. calc . array_like :: < _ , _ , ( ) > ( & element, Some ( count) ) ?
293
273
}
294
274
TyKind :: Slice ( element) => {
295
275
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
- }
276
+ cx. calc . array_like :: < _ , _ , ( ) > ( & element, None ) ?
277
+ }
278
+ TyKind :: Str => {
279
+ let element = scalar_unit ( dl, Primitive :: Int ( Integer :: I8 , false ) ) ;
280
+ cx. calc . array_like :: < _ , _ , ( ) > ( & Layout :: scalar ( dl, element) , None ) ?
308
281
}
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
282
// Potentially-wide pointers.
322
283
TyKind :: Ref ( _, _, pointee) | TyKind :: Raw ( _, pointee) => {
323
284
let mut data_ptr = scalar_unit ( dl, Primitive :: Pointer ( AddressSpace :: DATA ) ) ;
@@ -355,17 +316,12 @@ pub fn layout_of_ty_query(
355
316
} ;
356
317
357
318
// Effectively a (ptr, meta) tuple.
358
- cx . calc . scalar_pair ( data_ptr, metadata)
319
+ LayoutData :: scalar_pair ( dl , data_ptr, metadata)
359
320
}
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
321
+ TyKind :: Never => LayoutData :: never_type ( dl) ,
322
+ TyKind :: FnDef ( ..) | TyKind :: Dyn ( _) | TyKind :: Foreign ( _) => {
323
+ let sized = matches ! ( kind, TyKind :: FnDef ( ..) ) ;
324
+ LayoutData :: unit ( dl, sized)
369
325
}
370
326
TyKind :: Function ( _) => {
371
327
let mut ptr = scalar_unit ( dl, Primitive :: Pointer ( dl. instruction_address_space ) ) ;
@@ -434,16 +390,6 @@ pub fn layout_of_ty_recover(
434
390
Err ( LayoutError :: RecursiveTypeWithoutIndirection )
435
391
}
436
392
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
393
fn struct_tail_erasing_lifetimes ( db : & dyn HirDatabase , pointee : Ty ) -> Ty {
448
394
match pointee. kind ( Interner ) {
449
395
TyKind :: Adt ( AdtId ( hir_def:: AdtId :: StructId ( i) ) , subst) => {
@@ -474,9 +420,5 @@ fn scalar_unit(dl: &TargetDataLayout, value: Primitive) -> Scalar {
474
420
Scalar :: Initialized { value, valid_range : WrappingRange :: full ( value. size ( dl) ) }
475
421
}
476
422
477
- fn scalar ( dl : & TargetDataLayout , value : Primitive ) -> Layout {
478
- Layout :: scalar ( dl, scalar_unit ( dl, value) )
479
- }
480
-
481
423
#[ cfg( test) ]
482
424
mod tests;
0 commit comments