@@ -8,7 +8,9 @@ use rustc_middle::ty::layout::{
8
8
IntegerExt , LayoutCx , LayoutError , LayoutOf , TyAndLayout , MAX_SIMD_LANES ,
9
9
} ;
10
10
use 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
+ } ;
12
14
use rustc_session:: { DataTypeKind , FieldInfo , FieldKind , SizeKind , VariantInfo } ;
13
15
use rustc_span:: symbol:: Symbol ;
14
16
use rustc_target:: abi:: * ;
@@ -503,6 +505,23 @@ fn layout_of_uncached<'tcx>(
503
505
) ) ;
504
506
}
505
507
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
+
506
525
let get_discriminant_type =
507
526
|min, max| Integer :: repr_discr ( tcx, ty, & def. repr ( ) , min, max) ;
508
527
@@ -519,11 +538,8 @@ fn layout_of_uncached<'tcx>(
519
538
. iter_enumerated ( )
520
539
. any ( |( i, v) | v. discr != ty:: VariantDiscr :: Relative ( i. as_u32 ( ) ) ) ;
521
540
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) ;
527
543
528
544
let Some ( layout) = cx. layout_of_struct_or_enum (
529
545
& def. repr ( ) ,
0 commit comments