Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safe transmute: support non-ZST, variantful, uninhabited enums #126493

Merged
merged 1 commit into from
Jun 19, 2024

Commits on Jun 14, 2024

  1. safe transmute: support non-ZST, variantful, uninhabited enums

    Previously, `Tree::from_enum`'s implementation branched into three disjoint
    cases:
    
     1. enums that uninhabited
     2. enums for which all but one variant is uninhabited
     3. enums with multiple inhabited variants
    
    This branching (incorrectly) did not differentiate between variantful and
    variantless uninhabited enums. In both cases, we assumed (and asserted) that
    uninhabited enums are zero-sized types. This assumption is false for enums like:
    
        enum Uninhabited { A(!, u128) }
    
    ...which, currently, has the same size as `u128`. This faulty assumption
    manifested as the ICE reported in rust-lang#126460.
    
    In this PR, we revise the first case of `Tree::from_enum` to consider only the
    narrow category of "enums that are uninhabited ZSTs". These enums, whose layouts
    are described with `Variants::Single { index }`, are special in their layouts
    otherwise resemble the `!` type and cannot be descended into like typical enums.
    This first case captures uninhabited enums like:
    
        enum Uninhabited { A(!, !), B(!) }
    
    The second case is revised to consider the broader category of "enums that defer
    their layout to one of their variants"; i.e., enums whose layouts are described
    with `Variants::Single { index }` and that do have a variant at `index`. This
    second case captures uninhabited enums that are not ZSTs, like:
    
        enum Uninhabited { A(!, u128) }
    
    ...which represent their variants with `Variants::Single`.
    
    Finally, the third case is revised to cover the broader category of "enums with
    multiple variants", which captures uninhabited, non-ZST enums like:
    
        enum Uninhabited { A(u8, !), B(!, u32) }
    
    ...which represent their variants with `Variants::Multiple`.
    
    This PR also adds a comment requested by RalfJung in his review of rust-lang#126358 to
    `compiler/rustc_const_eval/src/interpret/discriminant.rs`.
    
    Fixes rust-lang#126460
    jswrenn committed Jun 14, 2024
    Configuration menu
    Copy the full SHA
    df1d616 View commit details
    Browse the repository at this point in the history