Skip to content

Commit

Permalink
Merge pull request #1317 from Nadrieril/ice
Browse files Browse the repository at this point in the history
Avoid an ICE by matching on type earlier
  • Loading branch information
W95Psp authored Feb 20, 2025
2 parents 26674cd + 08d8ba8 commit ba1a14a
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions frontend/exporter/src/constant_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,49 +647,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>>(
Expand Down

0 comments on commit ba1a14a

Please sign in to comment.