Commit 9a8545d590

Andrew Kelley <superjoe30@gmail.com>
2017-11-26 22:03:56
translate-c: fix translation when no default switch case
1 parent aa2ca3f
Changed files (2)
src/translate_c.cpp
@@ -2344,7 +2344,7 @@ static AstNode *trans_switch_stmt(Context *c, TransScope *parent_scope, const Sw
 
     if (!switch_scope->found_default && !stmt->isAllEnumCasesCovered()) {
         AstNode *prong_node = trans_create_node(c, NodeTypeSwitchProng);
-        prong_node->data.switch_prong.expr = trans_create_node(c, NodeTypeBreak);
+        prong_node->data.switch_prong.expr = trans_create_node_goto(c, end_label_name);
         switch_scope->switch_node->data.switch_expr.prongs.append(prong_node);
     }
 
test/translate_c.zig
@@ -1042,6 +1042,44 @@ pub fn addCases(cases: &tests.TranslateCContext) {
         \\    return x + 13;
         \\}
     );
+
+    cases.add("switch statement with no default",
+        \\int foo(int x) {
+        \\    switch (x) {
+        \\        case 1:
+        \\            x += 1;
+        \\        case 2:
+        \\            break;
+        \\        case 3:
+        \\        case 4:
+        \\            return x + 1;
+        \\    }
+        \\    return x + 13;
+        \\}
+    ,
+        \\fn foo(_arg_x: c_int) -> c_int {
+        \\    var x = _arg_x;
+        \\    {
+        \\        switch (x) {
+        \\            1 => goto case_0,
+        \\            2 => goto case_1,
+        \\            3 => goto case_2,
+        \\            4 => goto case_3,
+        \\            else => goto end,
+        \\        };
+        \\    case_0:
+        \\        x += 1;
+        \\    case_1:
+        \\        goto end;
+        \\    case_2:
+        \\    case_3:
+        \\        return x + 1;
+        \\        goto end;
+        \\    end:
+        \\    };
+        \\    return x + 13;
+        \\}
+    );
 }