@@ -396,6 +396,15 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
396396 }
397397 }
398398 }
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+
399408 let ity = if use_align {
400409 // Use the overall alignment
401410 match align {
@@ -813,11 +822,11 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
813822 // FIXME #10604: this breaks when vector types are present.
814823 let ( size, align) = union_size_and_align ( & sts[ ..] ) ;
815824 let align_s = align as u64 ;
816- assert_eq ! ( size % align_s, 0 ) ;
817- let align_units = size / align_s - 1 ;
818-
819825 let discr_ty = ll_inttype ( cx, ity) ;
820826 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;
821830 let fill_ty = match align_s {
822831 1 => Type :: array ( & Type :: i8 ( cx) , align_units) ,
823832 2 => Type :: array ( & Type :: i16 ( cx) , align_units) ,
@@ -829,10 +838,10 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
829838 _ => panic ! ( "unsupported enum alignment: {}" , align)
830839 } ;
831840 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
833842 let mut fields: Vec < Type > =
834843 [ discr_ty,
835- Type :: array ( & discr_ty, align_s / discr_size - 1 ) ,
844+ Type :: array ( & discr_ty, ( padded_discr_size - discr_size ) /discr_size ) ,
836845 fill_ty] . iter ( ) . cloned ( ) . collect ( ) ;
837846 if delay_drop_flag && dtor_needed {
838847 fields. pop ( ) ;
0 commit comments