rust/tests/ui/transmutability
Jack Wrenn df1d6168f4 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
2024-06-14 21:11:08 +00:00
..
abstraction safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
alignment Safe Transmute: lowercase diagnostics 2024-03-15 17:55:49 +00:00
arrays Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
enums safe transmute: support non-ZST, variantful, uninhabited enums 2024-06-14 21:11:08 +00:00
malformed-program-gracefulness Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
primitives Fix test problems discovered by the revision check 2024-05-09 14:47:09 +10:00
references Harmonize use of leaf and root obligation in trait error reporting 2024-06-12 20:57:23 -04:00
safety safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
structs Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
unions Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
issue-101739-1.rs safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
issue-101739-1.stderr safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
issue-101739-2.rs Mark all extraneous generic args as errors 2024-06-03 13:21:17 +00:00
issue-101739-2.stderr Mark all extraneous generic args as errors 2024-06-03 13:21:17 +00:00
issue-110467.rs safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
issue-110892.rs safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
issue-110892.stderr safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
maybeuninit.rs Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
maybeuninit.stderr Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
region-infer.rs safe transmute: revise safety analysis 2024-02-27 16:22:32 +00:00
region-infer.stderr Safe Transmute: lowercase diagnostics 2024-03-15 17:55:49 +00:00
transmute-padding-ice.rs Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
uninhabited.rs safe transmute: support non-ZST, variantful, uninhabited enums 2024-06-14 21:11:08 +00:00
uninhabited.stderr safe transmute: support non-ZST, variantful, uninhabited enums 2024-06-14 21:11:08 +00:00