Skip to content

Commit 2af3dd7

Browse files
committed
Auto merge of #32010 - devonhollowood:non-c-like-enum-repr, r=Aatch
Add tests for #26114 First step in fixing #26114
2 parents 978bc07 + b5be095 commit 2af3dd7

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/librustc_trans/trans/adt.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -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();

src/test/run-pass/enum-discrim-manual-sizing.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111

12-
use std::mem::size_of;
12+
use std::mem::{size_of, align_of};
1313

1414
#[repr(i8)]
1515
enum Ei8 {
@@ -71,6 +71,24 @@ enum Euint {
7171
Buint = 1
7272
}
7373

74+
#[repr(u8)]
75+
enum Eu8NonCLike<T> {
76+
_None,
77+
_Some(T),
78+
}
79+
80+
#[repr(i64)]
81+
enum Ei64NonCLike<T> {
82+
_None,
83+
_Some(T),
84+
}
85+
86+
#[repr(u64)]
87+
enum Eu64NonCLike<T> {
88+
_None,
89+
_Some(T),
90+
}
91+
7492
pub fn main() {
7593
assert_eq!(size_of::<Ei8>(), 1);
7694
assert_eq!(size_of::<Eu8>(), 1);
@@ -82,4 +100,17 @@ pub fn main() {
82100
assert_eq!(size_of::<Eu64>(), 8);
83101
assert_eq!(size_of::<Eint>(), size_of::<isize>());
84102
assert_eq!(size_of::<Euint>(), size_of::<usize>());
103+
assert_eq!(size_of::<Eu8NonCLike<()>>(), 1);
104+
assert_eq!(size_of::<Ei64NonCLike<()>>(), 8);
105+
assert_eq!(size_of::<Eu64NonCLike<()>>(), 8);
106+
let u8_expected_size = round_up(9, align_of::<Eu64NonCLike<u8>>());
107+
assert_eq!(size_of::<Eu64NonCLike<u8>>(), u8_expected_size);
108+
let array_expected_size = round_up(28, align_of::<Eu64NonCLike<[u32; 5]>>());
109+
assert_eq!(size_of::<Eu64NonCLike<[u32; 5]>>(), array_expected_size);
110+
assert_eq!(size_of::<Eu64NonCLike<[u32; 6]>>(), 32);
111+
}
112+
113+
// Rounds x up to the next multiple of a
114+
fn round_up(x: usize, a: usize) -> usize {
115+
((x + (a - 1)) / a) * a
85116
}

0 commit comments

Comments
 (0)