Skip to content

Suboptimal layout for nested enums #147341

@moulins

Description

@moulins

I tried this code:

pub enum Outer {
    A(Inner),
    B(u32, u8),
}

pub enum Inner {
    A(u32),
    B(u32),
}

fn main() {
  dbg!(std::mem::size_of::<Outer>());
}

I expected size_of::<Outer>() to be 8, indicating that rustc was able to "merge" the two enums:

  • Outer::A(Inner::A) gets discriminant 0;
  • Outer::B(Inner::B) gets discriminant 1;
  • Outer::B gets discriminant 2;

Instead, size_of::<Outer>() is 12, indicating that no layout optimization was done.

Meta

$ rustc +nightly --version
rustc 1.92.0-nightly (fa3155a64 2025-09-30)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions