Skip to content

Layout for enums does not match MIR (and types) very well #70399

@RalfJung

Description

@RalfJung

Enum layout computation entirely ignores ZST uninhabited ("absent") variants, which can lead to the layout being quite different in structure from the user-visible enum:

  • if there are more than 1 non-absent variants, Variants::Multiple is used.
  • if there is exactly 1 non-absent variant, Variants::Single is used.
  • if all variants are absent, the layout of ! is used, so the entire thing ends up looking like a union with 0 fields.

When actually asking for the layout of such an absent variant, it has to be made up "on the spot".

In terms of testing things like Miri (or anything else that needs to work with layout variants information), this leads to the "interesting" situation that we need an enum with at least 3 variants where 1 is "absent" to make sure that Variants::Multiple with absent variants is handled correctly.

This could be improved by generalizing Variants::Multiple to also support "discriminants" of "type" () and !, thus uniformly handling all enums.

Also see the second half of #69763 (comment), and discussion below #69763 (comment).

Cc @eddyb

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-layoutArea: Memory layout of typesC-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-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