@@ -400,6 +400,15 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
400400 }
401401 }
402402 }
403+
404+ // If the alignment is smaller than the chosen discriminant size, don't use the
405+ // alignment as the final size.
406+ let min_ty = ll_inttype ( & cx, min_ity) ;
407+ let min_size = machine:: llsize_of_real ( cx, min_ty) ;
408+ if ( align as u64 ) < min_size {
409+ use_align = false ;
410+ }
411+
403412 let ity = if use_align {
404413 // Use the overall alignment
405414 match align {
@@ -817,11 +826,11 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
817826 // FIXME #10604: this breaks when vector types are present.
818827 let ( size, align) = union_size_and_align ( & sts[ ..] ) ;
819828 let align_s = align as u64 ;
820- assert_eq ! ( size % align_s, 0 ) ;
821- let align_units = size / align_s - 1 ;
822-
823829 let discr_ty = ll_inttype ( cx, ity) ;
824830 let discr_size = machine:: llsize_of_alloc ( cx, discr_ty) ;
831+ let padded_discr_size = roundup ( discr_size, align) ;
832+ assert_eq ! ( size % align_s, 0 ) ; // Ensure division in align_units comes out evenly
833+ let align_units = ( size - padded_discr_size) / align_s;
825834 let fill_ty = match align_s {
826835 1 => Type :: array ( & Type :: i8 ( cx) , align_units) ,
827836 2 => Type :: array ( & Type :: i16 ( cx) , align_units) ,
@@ -833,10 +842,10 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
833842 _ => panic ! ( "unsupported enum alignment: {}" , align)
834843 } ;
835844 assert_eq ! ( machine:: llalign_of_min( cx, fill_ty) , align) ;
836- assert_eq ! ( align_s % discr_size, 0 ) ;
845+ assert_eq ! ( padded_discr_size % discr_size, 0 ) ; // Ensure discr_ty can fill pad evenly
837846 let mut fields: Vec < Type > =
838847 [ discr_ty,
839- Type :: array ( & discr_ty, align_s / discr_size - 1 ) ,
848+ Type :: array ( & discr_ty, ( padded_discr_size - discr_size ) /discr_size ) ,
840849 fill_ty] . iter ( ) . cloned ( ) . collect ( ) ;
841850 if delay_drop_flag && dtor_needed {
842851 fields. pop ( ) ;
0 commit comments