Skip to content

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

Closed

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)

Activity

  1. added
    T-langRelevant to the language team
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    A-layoutArea: Memory layout of types
    A-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))
    on Sep 24, 2025
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Sep 24, 2025
  3. changed the title [-]Uninhibited `repr(uX)` enum shouldn't have size zero[/-] [+]Uninhabited `repr(uX)` enum shouldn't have size zero[/+] on Sep 24, 2025
  4. zachs18 commented on Sep 24, 2025

    @zachs18
    Contributor

    @rustbot claim

  5. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 5, 2025
  6. added a commit that references this issue on Feb 28, 2026
  7. added a commit that references this issue on Mar 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))A-layoutArea: Memory layout of typesA-reprArea: the `#[repr(stuff)]` attributeC-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