rust-lang / rust-lang/rust

Suboptimal layout for nested enums

Open
#147,341 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-layout C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with the stated nightly rustc version and confirm the reported 12-byte layout for Outer. Start by investigating rustc's enum layout and discriminant handling; done means the nested enums receive the described merged layout and size_of::() reports 8, with appropriate compiler tests added.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.