Commit 86ec26b1f0

Evan Typanski <evantypanski@gmail.com>
2023-01-28 17:53:38
translate-c: Fix types on assign expression bool
1 parent 3c8d968
Changed files (2)
src/translate_c.zig
@@ -4519,7 +4519,10 @@ fn transCreateNodeAssign(
     defer block_scope.deinit();
 
     const tmp = try block_scope.makeMangledName(c, "tmp");
-    const rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
+    var rhs_node = try transExpr(c, &block_scope.base, rhs, .used);
+    if (!exprIsBooleanType(lhs) and isBoolRes(rhs_node)) {
+        rhs_node = try Tag.bool_to_int.create(c.arena, rhs_node);
+    }
     const tmp_decl = try Tag.var_simple.create(c.arena, .{ .name = tmp, .init = rhs_node });
     try block_scope.statements.append(tmp_decl);
 
test/translate_c.zig
@@ -3900,4 +3900,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
         \\pub const ZERO = @as(c_int, 0);
         \\pub const WORLD = @as(c_int, 0o0000123);
     });
+
+    cases.add("Assign expression from bool to int",
+        \\void foo(void) {
+        \\    int a;
+        \\    if (a = 1 > 0) {}
+        \\}
+    , &[_][]const u8{
+        \\pub export fn foo() void {
+        \\    var a: c_int = undefined;
+        \\    if ((blk: {
+        \\        const tmp = @boolToInt(@as(c_int, 1) > @as(c_int, 0));
+        \\        a = tmp;
+        \\        break :blk tmp;
+        \\    }) != 0) {}
+        \\}
+    });
 }