-
Notifications
You must be signed in to change notification settings - Fork 58
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
Semantics of SetDiscriminant with niched variants #487
Comments
|
That's news to me. Since when? How are enum variants set? |
As we do not deaggregate ADTs any more, we only construct enum variants using an aggregate assignment. So rustc does not produce SetDiscriminant on enums any more. |
Oh I see. Internally in the MIR semantics that will still be a bunch of assignments followed by SetDiscriminant though... but it's probably impossible to hit the "bad" case that way. Still, having SetDiscriminant "verify" the discriminant it wrote makes a lot of sense to me. |
rust-lang/rust#120882 implements that in Miri. |
…ler-errors interpret/write_discriminant: when encoding niched variant, ensure the stored value matches Cc rust-lang/unsafe-code-guidelines#487
Rollup merge of rust-lang#120882 - RalfJung:set-discriminant, r=compiler-errors interpret/write_discriminant: when encoding niched variant, ensure the stored value matches Cc rust-lang/unsafe-code-guidelines#487
interpret/write_discriminant: when encoding niched variant, ensure the stored value matches Cc rust-lang/unsafe-code-guidelines#487
This is motivated by rust-lang/rust#119674:
For a type like
enum E { A, B(char) }
, currentlySetDiscriminant(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 callingSetDiscriminant(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 ofread_discriminant
, and cause UB if the already encoded discriminant does not match the niched variant.Cc @cjgillot @tmiasko
The text was updated successfully, but these errors were encountered: