Skip to content

Uninhabited repr(uX) enum shouldn't have size zero #146984

@theemathas

Description

@theemathas

I tried this code:

#[derive(Clone, Copy)]
enum Never {}

#[repr(u32)]
enum MyEnum {
    A(Never),
    B(Never),
}

#[repr(C)]
union MyEnumRepr {
    A: MyVariantA,
    B: MyVariantB,
}

#[repr(u32)]
#[derive(Copy, Clone)]
enum MyEnumDiscriminant { A, B }

#[repr(C)]
#[derive(Clone, Copy)]
struct MyVariantA(MyEnumDiscriminant, Never);

#[repr(C)]
#[derive(Clone, Copy)]
struct MyVariantB(MyEnumDiscriminant, Never);

fn main() {
    println!("{}", size_of::<MyEnum>());
    println!("{}", size_of::<MyEnumRepr>());
}

I expected MyEnum and MyEnumRepr to have the same size, since this is the desugaring given as an example in the reference.

Instead, MyEnum has size 0, while MyEnumRepr has size 4.

Discovered by masterjason9637 on the rust community discord.

Meta

Reproducible on the playground with version 1.92.0-nightly (2025-09-23 975e6c8fec280816d24f)

Metadata

Metadata

Assignees

Labels

A-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))A-layoutArea: Memory layout of typesC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions