about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/const_eval
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2022-02-17 00:00:00 +0000
committerTomasz Miąsko <tomasz.miasko@gmail.com>2022-02-19 17:10:11 +0100
commitc2da477853e1bd8e3a6077d17efabc7dc9b1b792 (patch)
tree7446203105f981fce67a2e811490f02c88e895fa /compiler/rustc_const_eval/src/const_eval
parente08d5693609a659e45025b8ea4dbd9efa342fa68 (diff)
Fix pretty printing of enums without variants
92d20c4aaddea9507f8ad37fe37c551219153bbf removed no-variants special
case from try_destructure_const with expectation that this case would be
handled gracefully when read_discriminant returns an error.

Alas in that case read_discriminant succeeds while returning a
non-existing variant, so the special case is still necessary.
Diffstat (limited to 'compiler/rustc_const_eval/src/const_eval')
-rw-r--r--compiler/rustc_const_eval/src/const_eval/mod.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs
index ba1d5f45bbb..724f92243d0 100644
--- a/compiler/rustc_const_eval/src/const_eval/mod.rs
+++ b/compiler/rustc_const_eval/src/const_eval/mod.rs
@@ -147,6 +147,10 @@ pub(crate) fn try_destructure_const<'tcx>(
     // We go to `usize` as we cannot allocate anything bigger anyway.
     let (field_count, variant, down) = match val.ty().kind() {
         ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
+        // Checks if we have any variants, to avoid downcasting to a non-existing variant (when
+        // there are no variants `read_discriminant` successfully returns a non-existing variant
+        // index).
+        ty::Adt(def, _) if def.variants.is_empty() => throw_ub!(Unreachable),
         ty::Adt(def, _) => {
             let variant = ecx.read_discriminant(&op)?.1;
             let down = ecx.operand_downcast(&op, variant)?;