rust/tests/ui/transmutability
Jubilee d33ec8ed8c
Rollup merge of #126358 - jswrenn:fix-125811, r=compiler-errors
safe transmute: support `Single` enums

Previously, the implementation of `Tree::from_enum` incorrectly treated enums with `Variants::Single` and `Variants::Multiple` identically. This is incorrect for `Variants::Single` enums, which delegate their layout to that of a variant with a particular index (or no variant at all if the enum is empty).

This flaw manifested first as an ICE. `Tree::from_enum` attempted to compute the tag of variants other than the one at `Variants::Single`'s `index`, and fell afoul of a sanity-checking assertion in `compiler/rustc_const_eval/src/interpret/discriminant.rs`. This assertion is non-load-bearing, and can be removed; the routine its in is well-behaved even without it.

With the assertion removed, the proximate issue becomes apparent: calling `Tree::from_variant` on a variant that does not exist is ill-defined. A sanity check the given variant has `FieldShapes::Arbitrary` fails, and the analysis is (correctly) aborted with `Err::NotYetSupported`.

This commit corrects this chain of failures by ensuring that `Tree::from_variant` is not called on variants that are, as far as layout is concerned, nonexistent. Specifically, the implementation of `Tree::from_enum` is now partitioned into three cases:

  1. enums that are uninhabited
  2. enums for which all but one variant is uninhabited
  3. enums with multiple inhabited variants

`Tree::from_variant` is now only invoked in the third case. In the first case, `Tree::uninhabited()` is produced. In the second case, the layout is delegated to `Variants::Single`'s index.

Fixes #125811
2024-06-12 20:03:22 -07: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 Variants::Single enums 2024-06-13 01:38:51 +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 Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00
uninhabited.stderr Compute transmutability from rustc_target::abi::Layout 2024-04-08 15:36:52 +00:00