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 #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 #126358 to `compiler/rustc_const_eval/src/interpret/discriminant.rs`. Fixes #126460
This commit is contained in:
parent
f9515fdd5a
commit
df1d6168f4
5 changed files with 89 additions and 30 deletions
|
@ -341,37 +341,29 @@ pub(crate) mod rustc {
|
|||
|
||||
// We consider three kinds of enums, each demanding a different
|
||||
// treatment of their layout computation:
|
||||
// 1. enums that are uninhabited
|
||||
// 2. enums for which all but one variant is uninhabited
|
||||
// 3. enums with multiple inhabited variants
|
||||
// 1. enums that are uninhabited ZSTs
|
||||
// 2. enums that delegate their layout to a variant
|
||||
// 3. enums with multiple variants
|
||||
match layout.variants() {
|
||||
_ if layout.abi.is_uninhabited() => {
|
||||
// Uninhabited enums are usually (always?) zero-sized. In
|
||||
// the (unlikely?) event that an uninhabited enum is
|
||||
// non-zero-sized, this assert will trigger an ICE, and this
|
||||
// code should be modified such that a `layout.size` amount
|
||||
// of uninhabited bytes is returned instead.
|
||||
//
|
||||
// Uninhabited enums are currently implemented such that
|
||||
// their layout is described with `Variants::Single`, even
|
||||
// though they don't necessarily have a 'single' variant to
|
||||
// defer to. That said, we don't bother specifically
|
||||
// matching on `Variants::Single` in this arm because the
|
||||
// behavioral principles here remain true even if, for
|
||||
// whatever reason, the compiler describes an uninhabited
|
||||
// enum with `Variants::Multiple`.
|
||||
assert_eq!(layout.size, Size::ZERO);
|
||||
Variants::Single { .. }
|
||||
if layout.abi.is_uninhabited() && layout.size == Size::ZERO =>
|
||||
{
|
||||
// The layout representation of uninhabited, ZST enums is
|
||||
// defined to be like that of the `!` type, as opposed of a
|
||||
// typical enum. Consequently, they cannot be descended into
|
||||
// as if they typical enums. We therefore special-case this
|
||||
// scenario and simply return an uninhabited `Tree`.
|
||||
Ok(Self::uninhabited())
|
||||
}
|
||||
Variants::Single { index } => {
|
||||
// `Variants::Single` on non-uninhabited enums denotes that
|
||||
// `Variants::Single` on enums with variants denotes that
|
||||
// the enum delegates its layout to the variant at `index`.
|
||||
layout_of_variant(*index)
|
||||
}
|
||||
Variants::Multiple { tag_field, .. } => {
|
||||
// `Variants::Multiple` denotes an enum with multiple
|
||||
// inhabited variants. The layout of such an enum is the
|
||||
// disjunction of the layouts of its tagged variants.
|
||||
// variants. The layout of such an enum is the disjunction
|
||||
// of the layouts of its tagged variants.
|
||||
|
||||
// For enums (but not coroutines), the tag field is
|
||||
// currently always the first field of the layout.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue