Commit 621629e20d

Vexu <git@vexu.eu>
2019-12-29 16:09:31
translate-c-2 fix assertion failure rendering do while
1 parent f5e7d2d
Changed files (2)
src-self-hosted
test
src-self-hosted/translate_c.zig
@@ -1792,7 +1792,10 @@ fn transDoWhileLoop(
         // zig:   b;
         // zig:   if (!cond) break;
         // zig: }
-        break :blk (try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)).cast(ast.Node.Block).?;
+        const body = (try transStmt(rp, &loop_scope, ZigClangDoStmt_getBody(stmt), .unused, .r_value)).cast(ast.Node.Block).?;
+        // if this is used as an expression in Zig it needs to be immediately followed by a semicolon
+        _ = try appendToken(rp.c, .Semicolon, ";");
+        break :blk body;
     } else blk: {
         // the C statement is without a block, so we need to create a block to contain it.
         // c: do
test/translate_c.zig
@@ -2185,4 +2185,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
     , &[_][]const u8{
         \\pub const FOO = if (a) b else c;
     });
+
+    cases.add("do while as expr",
+        \\static void foo(void) {
+        \\    if (1)
+        \\        do {} while (0);
+        \\}
+    , &[_][]const u8{
+        \\pub fn foo() void {
+        \\    if (1 != 0) while (true) {
+        \\        if (!(0 != 0)) break;
+        \\    };
+        \\}
+    });
 }