Skip to content

Commit 85872cf

Browse files
committed
Inhibit all-absent-variant optimization for all enum reprs that inhibit layout optimization, not just repr(C).
1 parent c8905ea commit 85872cf

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
317317
always_sized: bool,
318318
) -> LayoutCalculatorResult<FieldIdx, VariantIdx, F> {
319319
let (present_first, present_second) = {
320-
let mut present_variants = variants
321-
.iter_enumerated()
322-
.filter_map(|(i, v)| if !repr.c() && absent(v) { None } else { Some(i) });
320+
let mut present_variants = variants.iter_enumerated().filter_map(|(i, v)| {
321+
if !repr.inhibit_enum_layout_opt() && absent(v) { None } else { Some(i) }
322+
});
323323
(present_variants.next(), present_variants.next())
324324
};
325325
let present_first = match present_first {

tests/ui/layout/enum.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,13 @@ enum ScalarPairDifferingSign { //~ERROR: abi: ScalarPair
2222
A(u8),
2323
B(i8),
2424
}
25+
26+
enum Never {}
27+
28+
// See https://github.com/rust-lang/rust/issues/146984
29+
#[rustc_layout(size)]
30+
#[repr(u32)]
31+
enum DefinedLayoutAllUninhabited { //~ERROR: size: Size(4 bytes)
32+
A(Never),
33+
B(Never),
34+
}

tests/ui/layout/enum.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@ error: abi: ScalarPair(Initialized { value: Int(I8, false), valid_range: 0..=1 }
1616
LL | enum ScalarPairDifferingSign {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: aborting due to 3 previous errors
19+
error: size: Size(4 bytes)
20+
--> $DIR/enum.rs:31:1
21+
|
22+
LL | enum DefinedLayoutAllUninhabited {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
2026

0 commit comments

Comments
 (0)