@@ -134,42 +134,32 @@ fn univariant_uninterned<'tcx>(
134134 cx : & LayoutCx < ' tcx > ,
135135 ty : Ty < ' tcx > ,
136136 fields : & IndexSlice < FieldIdx , TyAndLayout < ' tcx > > ,
137- repr : & ReprOptions ,
138137 kind : StructKind ,
139138) -> 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) )
146141}
147142
148143fn extract_const_value < ' tcx > (
149- const_ : ty:: Const < ' tcx > ,
150- ty : Ty < ' tcx > ,
151144 cx : & LayoutCx < ' tcx > ,
145+ ty : Ty < ' tcx > ,
146+ ct : ty:: Const < ' tcx > ,
152147) -> Result < ty:: Value < ' tcx > , & ' tcx LayoutError < ' tcx > > {
153- match const_ . kind ( ) {
148+ match ct . kind ( ) {
154149 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:?}" ) ;
169153 }
154+ Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) )
170155 }
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:?}" ) ;
173163 }
174164 }
175165}
@@ -194,10 +184,9 @@ fn layout_of_uncached<'tcx>(
194184 } ;
195185 let scalar = |value : Primitive | tcx. mk_layout ( LayoutData :: scalar ( cx, scalar_unit ( value) ) ) ;
196186
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+ } ;
201190 debug_assert ! ( !ty. has_non_region_infer( ) ) ;
202191
203192 Ok ( match * ty. kind ( ) {
@@ -210,12 +199,12 @@ fn layout_of_uncached<'tcx>(
210199 & mut layout. backend_repr
211200 {
212201 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 ) ?
214203 . try_to_bits ( tcx, cx. typing_env )
215204 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
216205 }
217206 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 ) ?
219208 . try_to_bits ( tcx, cx. typing_env )
220209 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
221210 if !include_end {
@@ -274,16 +263,11 @@ fn layout_of_uncached<'tcx>(
274263 data_ptr. valid_range_mut ( ) . start = 1 ;
275264 }
276265
277- let pointee = tcx. normalize_erasing_regions ( cx. typing_env , pointee) ;
278266 if pointee. is_sized ( tcx, cx. typing_env ) {
279267 return Ok ( tcx. mk_layout ( LayoutData :: scalar ( cx, data_ptr) ) ) ;
280268 }
281269
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 ( ) {
287271 let pointee_metadata = Ty :: new_projection ( tcx, metadata_def_id, [ pointee] ) ;
288272 let metadata_ty =
289273 match tcx. try_normalize_erasing_regions ( cx. typing_env , pointee_metadata) {
@@ -354,7 +338,7 @@ fn layout_of_uncached<'tcx>(
354338
355339 // Arrays and slices.
356340 ty:: Array ( element, count) => {
357- let count = extract_const_value ( count , ty, cx ) ?
341+ let count = extract_const_value ( cx , ty, count ) ?
358342 . try_to_target_usize ( tcx)
359343 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
360344
@@ -415,17 +399,10 @@ fn layout_of_uncached<'tcx>(
415399 } ) ,
416400
417401 // Odd unit types.
418- ty:: FnDef ( ..) => {
419- univariant ( IndexSlice :: empty ( ) , & ReprOptions :: default ( ) , StructKind :: AlwaysSized ) ?
420- }
402+ ty:: FnDef ( ..) => univariant ( IndexSlice :: empty ( ) , StructKind :: AlwaysSized ) ?,
421403 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 ) ?;
429406 match unit. backend_repr {
430407 BackendRepr :: Memory { ref mut sized } => * sized = false ,
431408 _ => bug ! ( ) ,
@@ -439,7 +416,6 @@ fn layout_of_uncached<'tcx>(
439416 let tys = args. as_closure ( ) . upvar_tys ( ) ;
440417 univariant (
441418 & tys. iter ( ) . map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
442- & ReprOptions :: default ( ) ,
443419 StructKind :: AlwaysSized ,
444420 ) ?
445421 }
@@ -448,7 +424,6 @@ fn layout_of_uncached<'tcx>(
448424 let tys = args. as_coroutine_closure ( ) . upvar_tys ( ) ;
449425 univariant (
450426 & tys. iter ( ) . map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
451- & ReprOptions :: default ( ) ,
452427 StructKind :: AlwaysSized ,
453428 ) ?
454429 }
@@ -457,11 +432,7 @@ fn layout_of_uncached<'tcx>(
457432 let kind =
458433 if tys. len ( ) == 0 { StructKind :: AlwaysSized } else { StructKind :: MaybeUnsized } ;
459434
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) ?
465436 }
466437
467438 // SIMD vector types.
@@ -719,25 +690,30 @@ fn layout_of_uncached<'tcx>(
719690 }
720691
721692 // Types with no meaningful known layout.
693+ ty:: Param ( _) => {
694+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
695+ }
696+
722697 ty:: Alias ( ..) => {
723- if ty. has_param ( ) {
724- return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
725- }
726698 // NOTE(eddyb) `layout_of` query should've normalized these away,
727699 // 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) ) ;
737708 }
738709
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}`" )
741717 }
742718 } )
743719}
@@ -911,13 +887,7 @@ fn coroutine_layout<'tcx>(
911887 . chain ( iter:: once ( Ok ( tag_layout) ) )
912888 . chain ( promoted_layouts)
913889 . 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 ) ?;
921891
922892 let ( prefix_size, prefix_align) = ( prefix. size , prefix. align ) ;
923893
@@ -982,7 +952,6 @@ fn coroutine_layout<'tcx>(
982952 cx,
983953 ty,
984954 & variant_only_tys. map ( |ty| cx. layout_of ( ty) ) . try_collect :: < IndexVec < _ , _ > > ( ) ?,
985- & ReprOptions :: default ( ) ,
986955 StructKind :: Prefixed ( prefix_size, prefix_align. abi ) ,
987956 ) ?;
988957 variant. variants = Variants :: Single { index } ;
0 commit comments