From 08d8ba8252b4a5128d107e797f15c494c5812bc7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 20 Feb 2025 01:38:48 +0100 Subject: [PATCH] Avoid an ICE by matching on type earlier --- frontend/exporter/src/constant_utils.rs | 80 ++++++++++++------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/frontend/exporter/src/constant_utils.rs b/frontend/exporter/src/constant_utils.rs index e35b3e986..9a5b727c2 100644 --- a/frontend/exporter/src/constant_utils.rs +++ b/frontend/exporter/src/constant_utils.rs @@ -632,49 +632,49 @@ mod rustc { span: rustc_span::Span, ) -> ConstantExpr { let tcx = s.base().tcx; - - let dc = tcx - .try_destructure_mir_constant_for_user_output(val, ty) - .s_unwrap(s); - - // Iterate over the fields, which should be values - // Below: we are mutually recursive with [const_value_to_constant_expr], - // which takes a [Const] as input, but it should be - // ok because we call it on a strictly smaller value. - let fields = dc - .fields - .iter() - .copied() - .map(|(val, ty)| const_value_to_constant_expr(s, ty, val, span)); - - // The type should be tuple - let hax_ty: Ty = ty.sinto(s); match ty.kind() { - ty::TyKind::Tuple(_) => { - assert!(dc.variant.is_none()); - let fields = fields.collect(); - ConstantExprKind::Tuple { fields } - } - ty::TyKind::Adt(adt_def, ..) => { - let variant = dc.variant.unwrap_or(rustc_target::abi::FIRST_VARIANT); - let variants_info = get_variant_information(adt_def, variant, s); - let fields = fields - .zip(&adt_def.variant(variant).fields) - .map(|(value, field)| ConstantFieldExpr { - field: field.did.sinto(s), - value, - }) - .collect(); - ConstantExprKind::Adt { - info: variants_info, - fields, - } - } - _ => { - fatal!(s[span], "Expected the type to be tuple or adt: {:?}", val) + ty::TyKind::Tuple(_) | ty::TyKind::Adt(..) => { + let dc = tcx + .try_destructure_mir_constant_for_user_output(val, ty) + .s_unwrap(s); + + // Iterate over the fields, which should be values + // Below: we are mutually recursive with [const_value_to_constant_expr], + // which takes a [Const] as input, but it should be + // ok because we call it on a strictly smaller value. + let fields = dc + .fields + .iter() + .copied() + .map(|(val, ty)| const_value_to_constant_expr(s, ty, val, span)); + + let kind = match ty.kind() { + ty::TyKind::Tuple(_) => { + assert!(dc.variant.is_none()); + let fields = fields.collect(); + ConstantExprKind::Tuple { fields } + } + ty::TyKind::Adt(adt_def, ..) => { + let variant = dc.variant.unwrap_or(rustc_target::abi::FIRST_VARIANT); + let variants_info = get_variant_information(adt_def, variant, s); + let fields = fields + .zip(&adt_def.variant(variant).fields) + .map(|(value, field)| ConstantFieldExpr { + field: field.did.sinto(s), + value, + }) + .collect(); + ConstantExprKind::Adt { + info: variants_info, + fields, + } + } + _ => unreachable!(), + }; + kind.decorate(ty.sinto(s), span.sinto(s)) } + _ => fatal!(s[span], "Expected the type to be tuple or adt: {:?}", val), } - .decorate(hax_ty, span.sinto(s)) } pub fn const_value_to_constant_expr<'tcx, S: UnderOwnerState<'tcx>>(