Suboptimal layout for nested enums
Open
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 discriminant0;Outer::B(Inner::B)gets discriminant1;Outer::Bgets discriminant2;
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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