Skip to content

#[repr(u16)] enum doesn't pack fields as expected #53657

@upsuper

Description

@upsuper

The following code:

#[repr(u16)] enum Foo { A, B(u32, u16) }
println!("{}", std::mem::size_of::<Foo>());

currently outputs 12, but Foo really only needs to take 8 bytes.

There are several alternatives which can correctly be packed into 8 bytes:

#[repr(u8)] enum Foo { A, B(u32, u16) }
#[repr(u16)] enum Foo { A, B(u16, u32) }

I see no reason why the first enum couldn't be packed into 8 bytes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-enumArea: Enums (discriminated unions, or more generally ADTs (algebraic data types))A-reprArea: the `#[repr(stuff)]` attributeA-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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