Skip to content

Semantics of SetDiscriminant with niched variants #487

Open
@RalfJung

Description

@RalfJung

This is motivated by rust-lang/rust#119674:

For a type like enum E { A, B(char) }, currently SetDiscriminant(place, 1) is just a NOP, since niche-encoded variants do not need to store the discriminant explicitly.

However, that means that SetDiscriminant; Discriminant doesn't necessarily return the just-written discriminant, even for enums: we might be calling SetDiscriminant(place, 1) on a place that doesn't actually carry the niched value, but it remains a NOP. That seems unfortunate.

I think it'd make sense if SetDiscriminant on niched variants were to do the equivalent of read_discriminant, and cause UB if the already encoded discriminant does not match the niched variant.

Cc @cjgillot @tmiasko

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions