@@ -134,42 +134,32 @@ fn univariant_uninterned<'tcx>(
134
134
cx : & LayoutCx < ' tcx > ,
135
135
ty : Ty < ' tcx > ,
136
136
fields : & IndexSlice < FieldIdx , TyAndLayout < ' tcx > > ,
137
- repr : & ReprOptions ,
138
137
kind : StructKind ,
139
138
) -> Result < LayoutData < FieldIdx , VariantIdx > , & ' tcx LayoutError < ' tcx > > {
140
- let pack = repr. pack ;
141
- if pack. is_some ( ) && repr. align . is_some ( ) {
142
- cx. tcx ( ) . dcx ( ) . bug ( "struct cannot be packed and aligned" ) ;
143
- }
144
-
145
- cx. calc . univariant ( fields, repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
139
+ let repr = ReprOptions :: default ( ) ;
140
+ cx. calc . univariant ( fields, & repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
146
141
}
147
142
148
143
fn extract_const_value < ' tcx > (
149
- const_ : ty:: Const < ' tcx > ,
150
- ty : Ty < ' tcx > ,
151
144
cx : & LayoutCx < ' tcx > ,
145
+ ty : Ty < ' tcx > ,
146
+ ct : ty:: Const < ' tcx > ,
152
147
) -> Result < ty:: Value < ' tcx > , & ' tcx LayoutError < ' tcx > > {
153
- match const_ . kind ( ) {
148
+ match ct . kind ( ) {
154
149
ty:: ConstKind :: Value ( cv) => Ok ( cv) ,
155
- ty:: ConstKind :: Error ( guar) => {
156
- return Err ( error ( cx, LayoutError :: ReferencesError ( guar) ) ) ;
157
- }
158
- ty:: ConstKind :: Param ( _) | ty:: ConstKind :: Expr ( _) => {
159
- if !const_. has_param ( ) {
160
- bug ! ( "no generic type found in the type: {ty:?}" ) ;
161
- }
162
- return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
163
- }
164
- ty:: ConstKind :: Unevaluated ( _) => {
165
- if !const_. has_param ( ) {
166
- return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
167
- } else {
168
- return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
150
+ ty:: ConstKind :: Param ( _) | ty:: ConstKind :: Expr ( _) | ty:: ConstKind :: Unevaluated ( _) => {
151
+ if !ct. has_param ( ) {
152
+ bug ! ( "failed to normalize const, but it is not generic: {ct:?}" ) ;
169
153
}
154
+ Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) )
170
155
}
171
- ty:: ConstKind :: Infer ( _) | ty:: ConstKind :: Bound ( ..) | ty:: ConstKind :: Placeholder ( _) => {
172
- bug ! ( "unexpected type: {ty:?}" ) ;
156
+ ty:: ConstKind :: Infer ( _)
157
+ | ty:: ConstKind :: Bound ( ..)
158
+ | ty:: ConstKind :: Placeholder ( _)
159
+ | ty:: ConstKind :: Error ( _) => {
160
+ // `ty::ConstKind::Error` is handled at the top of `layout_of_uncached`
161
+ // (via `ty.error_reported()`).
162
+ bug ! ( "layout_of: unexpected const: {ct:?}" ) ;
173
163
}
174
164
}
175
165
}
@@ -194,10 +184,9 @@ fn layout_of_uncached<'tcx>(
194
184
} ;
195
185
let scalar = |value : Primitive | tcx. mk_layout ( LayoutData :: scalar ( cx, scalar_unit ( value) ) ) ;
196
186
197
- let univariant =
198
- |fields : & IndexSlice < FieldIdx , TyAndLayout < ' tcx > > , repr : & ReprOptions , kind| {
199
- Ok ( tcx. mk_layout ( univariant_uninterned ( cx, ty, fields, repr, kind) ?) )
200
- } ;
187
+ let univariant = |fields : & IndexSlice < FieldIdx , TyAndLayout < ' tcx > > , kind| {
188
+ Ok ( tcx. mk_layout ( univariant_uninterned ( cx, ty, fields, kind) ?) )
189
+ } ;
201
190
debug_assert ! ( !ty. has_non_region_infer( ) ) ;
202
191
203
192
Ok ( match * ty. kind ( ) {
@@ -210,12 +199,12 @@ fn layout_of_uncached<'tcx>(
210
199
& mut layout. backend_repr
211
200
{
212
201
if let Some ( start) = start {
213
- scalar. valid_range_mut ( ) . start = extract_const_value ( start , ty, cx ) ?
202
+ scalar. valid_range_mut ( ) . start = extract_const_value ( cx , ty, start ) ?
214
203
. try_to_bits ( tcx, cx. typing_env )
215
204
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
216
205
}
217
206
if let Some ( end) = end {
218
- let mut end = extract_const_value ( end , ty, cx ) ?
207
+ let mut end = extract_const_value ( cx , ty, end ) ?
219
208
. try_to_bits ( tcx, cx. typing_env )
220
209
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
221
210
if !include_end {
@@ -274,16 +263,11 @@ fn layout_of_uncached<'tcx>(
274
263
data_ptr. valid_range_mut ( ) . start = 1 ;
275
264
}
276
265
277
- let pointee = tcx. normalize_erasing_regions ( cx. typing_env , pointee) ;
278
266
if pointee. is_sized ( tcx, cx. typing_env ) {
279
267
return Ok ( tcx. mk_layout ( LayoutData :: scalar ( cx, data_ptr) ) ) ;
280
268
}
281
269
282
- let metadata = if let Some ( metadata_def_id) = tcx. lang_items ( ) . metadata_type ( )
283
- // Projection eagerly bails out when the pointee references errors,
284
- // fall back to structurally deducing metadata.
285
- && !pointee. references_error ( )
286
- {
270
+ let metadata = if let Some ( metadata_def_id) = tcx. lang_items ( ) . metadata_type ( ) {
287
271
let pointee_metadata = Ty :: new_projection ( tcx, metadata_def_id, [ pointee] ) ;
288
272
let metadata_ty =
289
273
match tcx. try_normalize_erasing_regions ( cx. typing_env , pointee_metadata) {
@@ -354,7 +338,7 @@ fn layout_of_uncached<'tcx>(
354
338
355
339
// Arrays and slices.
356
340
ty:: Array ( element, count) => {
357
- let count = extract_const_value ( count , ty, cx ) ?
341
+ let count = extract_const_value ( cx , ty, count ) ?
358
342
. try_to_target_usize ( tcx)
359
343
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
360
344
@@ -415,17 +399,10 @@ fn layout_of_uncached<'tcx>(
415
399
} ) ,
416
400
417
401
// Odd unit types.
418
- ty:: FnDef ( ..) => {
419
- univariant ( IndexSlice :: empty ( ) , & ReprOptions :: default ( ) , StructKind :: AlwaysSized ) ?
420
- }
402
+ ty:: FnDef ( ..) => univariant ( IndexSlice :: empty ( ) , StructKind :: AlwaysSized ) ?,
421
403
ty:: Dynamic ( _, _, ty:: Dyn ) | ty:: Foreign ( ..) => {
422
- let mut unit = univariant_uninterned (
423
- cx,
424
- ty,
425
- IndexSlice :: empty ( ) ,
426
- & ReprOptions :: default ( ) ,
427
- StructKind :: AlwaysSized ,
428
- ) ?;
404
+ let mut unit =
405
+ univariant_uninterned ( cx, ty, IndexSlice :: empty ( ) , StructKind :: AlwaysSized ) ?;
429
406
match unit. backend_repr {
430
407
BackendRepr :: Memory { ref mut sized } => * sized = false ,
431
408
_ => bug ! ( ) ,
@@ -439,7 +416,6 @@ fn layout_of_uncached<'tcx>(
439
416
let tys = args. as_closure ( ) . upvar_tys ( ) ;
440
417
univariant (
441
418
& tys. iter ( ) . map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
442
- & ReprOptions :: default ( ) ,
443
419
StructKind :: AlwaysSized ,
444
420
) ?
445
421
}
@@ -448,7 +424,6 @@ fn layout_of_uncached<'tcx>(
448
424
let tys = args. as_coroutine_closure ( ) . upvar_tys ( ) ;
449
425
univariant (
450
426
& tys. iter ( ) . map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
451
- & ReprOptions :: default ( ) ,
452
427
StructKind :: AlwaysSized ,
453
428
) ?
454
429
}
@@ -457,11 +432,7 @@ fn layout_of_uncached<'tcx>(
457
432
let kind =
458
433
if tys. len ( ) == 0 { StructKind :: AlwaysSized } else { StructKind :: MaybeUnsized } ;
459
434
460
- univariant (
461
- & tys. iter ( ) . map ( |k| cx. layout_of ( k) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
462
- & ReprOptions :: default ( ) ,
463
- kind,
464
- ) ?
435
+ univariant ( & tys. iter ( ) . map ( |k| cx. layout_of ( k) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?, kind) ?
465
436
}
466
437
467
438
// SIMD vector types.
@@ -719,25 +690,30 @@ fn layout_of_uncached<'tcx>(
719
690
}
720
691
721
692
// Types with no meaningful known layout.
693
+ ty:: Param ( _) => {
694
+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
695
+ }
696
+
722
697
ty:: Alias ( ..) => {
723
- if ty. has_param ( ) {
724
- return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
725
- }
726
698
// NOTE(eddyb) `layout_of` query should've normalized these away,
727
699
// if that was possible, so there's no reason to try again here.
728
- return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
729
- }
730
-
731
- ty:: Bound ( ..) | ty:: CoroutineWitness ( ..) | ty:: Infer ( _) | ty:: Error ( _) => {
732
- bug ! ( "Layout::compute: unexpected type `{}`" , ty)
733
- }
734
-
735
- ty:: Param ( _) => {
736
- return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
700
+ let err = if ty. has_param ( ) {
701
+ LayoutError :: TooGeneric ( ty)
702
+ } else {
703
+ // This is only reachable with unsatisfiable predicates. For example, if we have
704
+ // `u8: Iterator`, then we can't compute the layout of `<u8 as Iterator>::Item`.
705
+ LayoutError :: Unknown ( ty)
706
+ } ;
707
+ return Err ( error ( cx, err) ) ;
737
708
}
738
709
739
- ty:: Placeholder ( ..) => {
740
- return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
710
+ ty:: Placeholder ( ..)
711
+ | ty:: Bound ( ..)
712
+ | ty:: CoroutineWitness ( ..)
713
+ | ty:: Infer ( _)
714
+ | ty:: Error ( _) => {
715
+ // `ty::Error` is handled at the top of this function.
716
+ bug ! ( "layout_of: unexpected type `{ty}`" )
741
717
}
742
718
} )
743
719
}
@@ -911,13 +887,7 @@ fn coroutine_layout<'tcx>(
911
887
. chain ( iter:: once ( Ok ( tag_layout) ) )
912
888
. chain ( promoted_layouts)
913
889
. try_collect :: < IndexVec < _ , _ > > ( ) ?;
914
- let prefix = univariant_uninterned (
915
- cx,
916
- ty,
917
- & prefix_layouts,
918
- & ReprOptions :: default ( ) ,
919
- StructKind :: AlwaysSized ,
920
- ) ?;
890
+ let prefix = univariant_uninterned ( cx, ty, & prefix_layouts, StructKind :: AlwaysSized ) ?;
921
891
922
892
let ( prefix_size, prefix_align) = ( prefix. size , prefix. align ) ;
923
893
@@ -982,7 +952,6 @@ fn coroutine_layout<'tcx>(
982
952
cx,
983
953
ty,
984
954
& variant_only_tys. map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
985
- & ReprOptions :: default ( ) ,
986
955
StructKind :: Prefixed ( prefix_size, prefix_align. abi ) ,
987
956
) ?;
988
957
variant. variants = Variants :: Single { index } ;
0 commit comments