@@ -8,7 +8,9 @@ use rustc_middle::ty::layout::{
88 IntegerExt , LayoutCx , LayoutError , LayoutOf , TyAndLayout , MAX_SIMD_LANES ,
99} ;
1010use rustc_middle:: ty:: print:: with_no_trimmed_paths;
11- use rustc_middle:: ty:: { self , AdtDef , EarlyBinder , GenericArgsRef , Ty , TyCtxt , TypeVisitableExt } ;
11+ use rustc_middle:: ty:: {
12+ self , AdtDef , EarlyBinder , FieldDef , GenericArgsRef , Ty , TyCtxt , TypeVisitableExt ,
13+ } ;
1214use rustc_session:: { DataTypeKind , FieldInfo , FieldKind , SizeKind , VariantInfo } ;
1315use rustc_span:: symbol:: Symbol ;
1416use rustc_target:: abi:: * ;
@@ -503,6 +505,23 @@ fn layout_of_uncached<'tcx>(
503505 ) ) ;
504506 }
505507
508+ let is_unsized_field = |field : & FieldDef | {
509+ let param_env = tcx. param_env ( def. did ( ) ) ;
510+ !tcx. type_of ( field. did ) . instantiate_identity ( ) . is_sized ( tcx, param_env)
511+ } ;
512+
513+ if def. is_struct ( )
514+ && let Some ( ( _, fields_except_last) ) =
515+ def. non_enum_variant ( ) . fields . raw . split_last ( )
516+ && fields_except_last. iter ( ) . any ( is_unsized_field)
517+ {
518+ cx. tcx . dcx ( ) . span_delayed_bug (
519+ tcx. def_span ( def. did ( ) ) ,
520+ "only the last field of a struct can be unsized" ,
521+ ) ;
522+ return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
523+ }
524+
506525 let get_discriminant_type =
507526 |min, max| Integer :: repr_discr ( tcx, ty, & def. repr ( ) , min, max) ;
508527
@@ -519,11 +538,8 @@ fn layout_of_uncached<'tcx>(
519538 . iter_enumerated ( )
520539 . any ( |( i, v) | v. discr != ty:: VariantDiscr :: Relative ( i. as_u32 ( ) ) ) ;
521540
522- let maybe_unsized = def. is_struct ( )
523- && def. non_enum_variant ( ) . tail_opt ( ) . is_some_and ( |last_field| {
524- let param_env = tcx. param_env ( def. did ( ) ) ;
525- !tcx. type_of ( last_field. did ) . instantiate_identity ( ) . is_sized ( tcx, param_env)
526- } ) ;
541+ let maybe_unsized =
542+ def. is_struct ( ) && def. non_enum_variant ( ) . tail_opt ( ) . is_some_and ( is_unsized_field) ;
527543
528544 let Some ( layout) = cx. layout_of_struct_or_enum (
529545 & def. repr ( ) ,
0 commit comments