From c2da477853e1bd8e3a6077d17efabc7dc9b1b792 Mon Sep 17 00:00:00 2001 From: Tomasz Miąsko Date: Thu, 17 Feb 2022 00:00:00 +0000 Subject: 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. --- compiler/rustc_const_eval/src/const_eval/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'compiler/rustc_const_eval/src/const_eval') 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)?; -- cgit 1.4.1-3-g733a5