Commit 50457482b1

february cozzocrea <91439207+f-cozzocrea@users.noreply.github.com>
2024-01-16 16:57:31
translate-c: Fix for compound assign implicit cast error
1 parent 195eeed
Changed files (2)
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);
         }
 
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