From 621629e20d7114d09761b49d8fb2bba203aadd1a Mon Sep 17 00:00:00 2001 From: Vexu Date: Sun, 29 Dec 2019 17:09:31 +0200 Subject: [PATCH] translate-c-2 fix assertion failure rendering do while --- src-self-hosted/translate_c.zig | 5 ++++- test/translate_c.zig | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 34c90a34d6..f87e826cbd 100644 --- a/src-self-hosted/translate_c.zig +++ b/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 diff --git a/test/translate_c.zig b/test/translate_c.zig index 625ecb53a0..5f91d95990 100644 --- a/test/translate_c.zig +++ b/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; + \\ }; + \\} + }); }