Skip to content

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

Open
@lolbinarycat

Description

@lolbinarycat

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

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