Skip to content

Commit 9484b2a

Browse files
committed
transpile: pass down modified override_ty for ArrayToPointerDecay casts
1 parent 65c0865 commit 9484b2a

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

  • c2rust-transpile/src/translator

c2rust-transpile/src/translator/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3778,10 +3778,38 @@ impl<'c> Translation<'c> {
37783778
return self.convert_expr(ctx, expr, override_ty);
37793779
}
37803780
}
3781+
if kind == CastKind::ArrayToPointerDecay && override_ty.is_some() {
3782+
let target_elem = match &self
3783+
.ast_context
3784+
.resolve_type(override_ty.unwrap().ctype)
3785+
.kind
3786+
{
3787+
CTypeKind::Pointer(qtype) => qtype.ctype,
3788+
k => panic!("ArrayToPointerDecay producing non-pointer type {k:?}"),
3789+
};
3790+
let new_array_override_ty_kind =
3791+
match &self.ast_context.resolve_type(source_ty.ctype).kind {
3792+
CTypeKind::ConstantArray(_elem, len) => {
3793+
CTypeKind::ConstantArray(target_elem, *len)
3794+
}
3795+
k => panic!("ArrayToPointerDecay on non-array type {k:?}"),
3796+
};
3797+
//
3798+
// && Some(source_ty.ctype) != override_ty.map(|x| x.ctype)
3799+
let new_array_override_ty = self
3800+
.ast_context
3801+
.type_for_kind(&new_array_override_ty_kind)
3802+
.expect(&format!(
3803+
"type id did not already exist for {:?}",
3804+
new_array_override_ty_kind
3805+
));
3806+
3807+
self.convert_expr(ctx, expr, Some(CQualTypeId::new(new_array_override_ty)))?
3808+
}
37813809
// LValueToRValue casts don't actually change the type, so it still makes sense
37823810
// to translate their inner expression with the expected type from outside the
37833811
// cast.
3784-
if kind == CastKind::LValueToRValue
3812+
else if kind == CastKind::LValueToRValue
37853813
&& Some(source_ty.ctype) != override_ty.map(|x| x.ctype)
37863814
{
37873815
self.convert_expr(ctx, expr, override_ty)?

0 commit comments

Comments
 (0)