Commit 5a2cbe239f

Andrew Kelley <andrew@ziglang.org>
2019-08-16 19:00:48
fix and test case for returning from suspend block
See #3063
1 parent 2cb1f93
Changed files (3)
src
test
stage1
behavior
src/codegen.cpp
@@ -5470,7 +5470,9 @@ static LLVMValueRef ir_render_assert_non_null(CodeGen *g, IrExecutable *executab
 static LLVMValueRef ir_render_suspend_begin(CodeGen *g, IrExecutable *executable,
         IrInstructionSuspendBegin *instruction)
 {
-    instruction->resume_bb = gen_suspend_begin(g, "SuspendResume");
+    if (fn_is_async(g->cur_fn)) {
+        instruction->resume_bb = gen_suspend_begin(g, "SuspendResume");
+    }
     return nullptr;
 }
 
src/ir.cpp
@@ -7914,7 +7914,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
         ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, node->data.suspend.block, susp_res));
     }
 
-    return ir_build_suspend_finish(irb, parent_scope, node, begin);
+    return ir_mark_gen(ir_build_suspend_finish(irb, parent_scope, node, begin));
 }
 
 static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope,
test/stage1/behavior/async_fn.zig
@@ -760,3 +760,17 @@ test "async call a generic function" {
     };
     _ = async S.doTheTest();
 }
+
+test "return from suspend block" {
+    const S = struct {
+        fn doTheTest() void {
+            expect(func() == 1234);
+        }
+        fn func() i32 {
+            suspend {
+                return 1234;
+            }
+        }
+    };
+    _ = async S.doTheTest();
+}