From 50457482b16dc402ad0eff9f7990258257e2dd62 Mon Sep 17 00:00:00 2001 From: february cozzocrea <91439207+f-cozzocrea@users.noreply.github.com> Date: Tue, 16 Jan 2024 07:57:31 -0800 Subject: [PATCH] translate-c: Fix for compound assign implicit cast error --- src/translate_c.zig | 14 +++++-------- ...compound_assignments_with_implicit_casts.c | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 test/cases/run_translated_c/compound_assignments_with_implicit_casts.c diff --git a/src/translate_c.zig b/src/translate_c.zig index e6305feec3..5683855d23 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -3807,11 +3807,7 @@ fn transCreateCompoundAssign( const rhs_qt = getExprQualType(c, rhs); const is_signed = cIsSignedInteger(lhs_qt); const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt); - const requires_int_cast = blk: { - const are_integers = cIsInteger(lhs_qt) and cIsInteger(rhs_qt); - const are_same_sign = cIsSignedInteger(lhs_qt) == cIsSignedInteger(rhs_qt); - break :blk are_integers and !(are_same_sign and cIntTypeCmp(lhs_qt, rhs_qt) == .eq); - }; + const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed; if (used == .unused) { // common case @@ -3822,7 +3818,7 @@ fn transCreateCompoundAssign( if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node); if ((is_mod or is_div) and is_signed) { - if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); + if (requires_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); const operands = .{ .lhs = lhs_node, .rhs = rhs_node }; const builtin = if (is_mod) try Tag.signed_remainder.create(c.arena, operands) @@ -3834,7 +3830,7 @@ fn transCreateCompoundAssign( if (is_shift) { rhs_node = try Tag.int_cast.create(c.arena, rhs_node); - } else if (requires_int_cast) { + } else if (requires_cast) { rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); } return transCreateNodeInfixOp(c, op, lhs_node, rhs_node, .used); @@ -3861,7 +3857,7 @@ fn transCreateCompoundAssign( var rhs_node = try transExpr(c, &block_scope.base, rhs, .used); if (is_ptr_op_signed) rhs_node = try usizeCastForWrappingPtrArithmetic(c.arena, rhs_node); if ((is_mod or is_div) and is_signed) { - if (requires_int_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); + if (requires_cast) rhs_node = try transCCast(c, scope, loc, lhs_qt, rhs_qt, rhs_node); const operands = .{ .lhs = ref_node, .rhs = rhs_node }; const builtin = if (is_mod) try Tag.signed_remainder.create(c.arena, operands) @@ -3873,7 +3869,7 @@ fn transCreateCompoundAssign( } else { if (is_shift) { rhs_node = try Tag.int_cast.create(c.arena, rhs_node); - } else if (requires_int_cast) { + } else if (requires_cast) { rhs_node = try transCCast(c, &block_scope.base, loc, lhs_qt, rhs_qt, rhs_node); } diff --git a/test/cases/run_translated_c/compound_assignments_with_implicit_casts.c b/test/cases/run_translated_c/compound_assignments_with_implicit_casts.c new file mode 100644 index 0000000000..c1cadfd925 --- /dev/null +++ b/test/cases/run_translated_c/compound_assignments_with_implicit_casts.c @@ -0,0 +1,20 @@ +int main() { + int i = 2; + float f = 3.2f; + + i += 1.7; + if (i != 3) return 1; + i += f; + if (i != 6) return 2; + + + f += 2UL; + if (f <= 5.1999 || f >= 5.2001) return 3; + f += i; + if (f <= 11.1999 || f >= 11.2001) return 4; + + return 0; +} + +// run-translated-c +// c_frontends=aro,clang