Skip to content

Commit 5a09f62

Browse files
committed
transpile: Move ctx modifications down in cast handling code
1 parent 3fc1d4a commit 5a09f62

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

  • c2rust-transpile/src/translator

c2rust-transpile/src/translator/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,14 +3078,26 @@ impl<'c> Translation<'c> {
30783078
ImplicitCast(ty, expr, kind, opt_field_id, _)
30793079
| ExplicitCast(ty, expr, kind, opt_field_id, _) => {
30803080
let is_explicit = matches!(expr_kind, CExprKind::ExplicitCast(..));
3081-
// A reference must be decayed if a bitcast is required. Const casts in
3082-
// LLVM 8 are now NoOp casts, so we need to include it as well.
3081+
let target_ty = override_ty.unwrap_or(ty);
3082+
3083+
// In general, if we are casting the result of an expression, then the inner
3084+
// expression should be translated to whatever type it normally would.
3085+
// But for some expression types, if we don't absolutely have to cast,
3086+
// we would rather the expression is translated according to the type we're
3087+
// expecting, and then we can skip the cast entirely.
3088+
if self.can_propagate_cast(expr, target_ty, is_explicit) {
3089+
return self.convert_expr(ctx, expr, Some(target_ty));
3090+
}
3091+
30833092
match kind {
30843093
CastKind::IntegralToBoolean
30853094
| CastKind::FloatingToBoolean
30863095
| CastKind::PointerToBoolean => {
30873096
return self.convert_condition(ctx, true, expr);
30883097
}
3098+
3099+
// A reference must be decayed if a bitcast is required. Const casts in
3100+
// LLVM 8 are now NoOp casts, so we need to include it as well.
30893101
CastKind::BitCast | CastKind::PointerToIntegral | CastKind::NoOp => {
30903102
ctx.decay_ref = DecayRef::Yes
30913103
}
@@ -3097,17 +3109,6 @@ impl<'c> Translation<'c> {
30973109
_ => {}
30983110
}
30993111

3100-
let target_ty = override_ty.unwrap_or(ty);
3101-
3102-
// In general, if we are casting the result of an expression, then the inner
3103-
// expression should be translated to whatever type it normally would.
3104-
// But for some expression types, if we don't absolutely have to cast,
3105-
// we would rather the expression is translated according to the type we're
3106-
// expecting, and then we can skip the cast entirely.
3107-
if self.can_propagate_cast(expr, target_ty, is_explicit) {
3108-
return self.convert_expr(ctx, expr, Some(target_ty));
3109-
}
3110-
31113112
let mut val = self.convert_expr(ctx, expr, None)?;
31123113

31133114
if is_explicit {

0 commit comments

Comments
 (0)