rust-lang / rust-lang/rust

Enum layout should allow multiple untagged variants if they have nonoverlapping values

Open
#139,148 0 comments 3 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:

enum En1 {
    Bool(bool),    
    Other(En2),
}

#[repr(u8)]
enum En2 {
    Six = 6,
    Seven = 7,
}

fn main() {
    dbg!(size_of::<En1>());
}

I expected to see this happen: En1 has size 1

Instead, this happened: En1 has size 2

This will become more of an issue with range types on the horizon

Meta

rustc --version --verbose:

1.85.1

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

Start by compiling the Rust reproducer in the issue and confirming the reported size of En1. Investigate the compiler's enum layout handling for untagged variants with nonoverlapping values; done means the example reports size 1 and the behavior is covered against regression.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.