about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/snippet.rs
diff options
context:
space:
mode:
authorJack Wrenn <jack@wrenn.fyi>2024-06-12 22:07:37 +0000
committerJack Wrenn <jack@wrenn.fyi>2024-06-13 01:38:51 +0000
commitfb662f2126b026c91ddbc17ac3bdb8bd2bf575c5 (patch)
tree1293de563facf958e14eaf1676706bd98aba31c8 /compiler/rustc_errors/src/snippet.rs
parent1d43fbbc7348f2bd9260d8532bffa02f5bd2c9ac (diff)
downloadrust-fb662f2126b026c91ddbc17ac3bdb8bd2bf575c5.tar.gz
rust-fb662f2126b026c91ddbc17ac3bdb8bd2bf575c5.zip
safe transmute: support `Variants::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
Diffstat (limited to 'compiler/rustc_errors/src/snippet.rs')
0 files changed, 0 insertions, 0 deletions