@@ -396,6 +396,15 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
396
396
}
397
397
}
398
398
}
399
+
400
+ // If the alignment is smaller than the chosen discriminant size, don't use the
401
+ // alignment as the final size.
402
+ let min_ty = ll_inttype ( & cx, min_ity) ;
403
+ let min_size = machine:: llsize_of_real ( cx, min_ty) ;
404
+ if ( align as u64 ) < min_size {
405
+ use_align = false ;
406
+ }
407
+
399
408
let ity = if use_align {
400
409
// Use the overall alignment
401
410
match align {
@@ -813,11 +822,11 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
813
822
// FIXME #10604: this breaks when vector types are present.
814
823
let ( size, align) = union_size_and_align ( & sts[ ..] ) ;
815
824
let align_s = align as u64 ;
816
- assert_eq ! ( size % align_s, 0 ) ;
817
- let align_units = size / align_s - 1 ;
818
-
819
825
let discr_ty = ll_inttype ( cx, ity) ;
820
826
let discr_size = machine:: llsize_of_alloc ( cx, discr_ty) ;
827
+ let padded_discr_size = roundup ( discr_size, align) ;
828
+ assert_eq ! ( size % align_s, 0 ) ; // Ensure division in align_units comes out evenly
829
+ let align_units = ( size - padded_discr_size) / align_s;
821
830
let fill_ty = match align_s {
822
831
1 => Type :: array ( & Type :: i8 ( cx) , align_units) ,
823
832
2 => Type :: array ( & Type :: i16 ( cx) , align_units) ,
@@ -829,10 +838,10 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
829
838
_ => panic ! ( "unsupported enum alignment: {}" , align)
830
839
} ;
831
840
assert_eq ! ( machine:: llalign_of_min( cx, fill_ty) , align) ;
832
- assert_eq ! ( align_s % discr_size, 0 ) ;
841
+ assert_eq ! ( padded_discr_size % discr_size, 0 ) ; // Ensure discr_ty can fill pad evenly
833
842
let mut fields: Vec < Type > =
834
843
[ discr_ty,
835
- Type :: array ( & discr_ty, align_s / discr_size - 1 ) ,
844
+ Type :: array ( & discr_ty, ( padded_discr_size - discr_size ) /discr_size ) ,
836
845
fill_ty] . iter ( ) . cloned ( ) . collect ( ) ;
837
846
if delay_drop_flag && dtor_needed {
838
847
fields. pop ( ) ;
0 commit comments