authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-02 14:16:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-02 14:16:46-04:00
logfb05b96492f4fb1476106bf735788ac16f69c7ef
tree14ec51f41d29214e550317f283aa8dcdbf223b9d
parent9ecbabfc4ce857e43db2b056bc83272cb24b0bbd
parent895f262a55b9951647efef4528c17cf64d6b7c07

Merge branch 'kristate-handle-builtin-issue1296'


21 files changed, 189 insertions(+), 130 deletions(-)

doc/langref.html.in+18-8
......@@ -4690,9 +4690,9 @@ test "coroutine suspend with block" {
46904690var a_promise: promise = undefined;
46914691var result = false;
46924692async fn testSuspendBlock() void {
4693 suspend |p| {
4694 comptime assert(@typeOf(p) == promise->void);
4695 a_promise = p;
4693 suspend {
4694 comptime assert(@typeOf(@handle()) == promise->void);
4695 a_promise = @handle();
46964696 }
46974697 result = true;
46984698}
......@@ -4733,8 +4733,8 @@ test "resume from suspend" {
47334733 std.debug.assert(my_result == 2);
47344734}
47354735async fn testResumeFromSuspend(my_result: *i32) void {
4736 suspend |p| {
4737 resume p;
4736 suspend {
4737 resume @handle();
47384738 }
47394739 my_result.* += 1;
47404740 suspend;
......@@ -4791,9 +4791,9 @@ async fn amain() void {
47914791}
47924792async fn another() i32 {
47934793 seq('c');
4794 suspend |p| {
4794 suspend {
47954795 seq('d');
4796 a_promise = p;
4796 a_promise = @handle();
47974797 }
47984798 seq('g');
47994799 return 1234;
......@@ -5383,6 +5383,16 @@ test "main" {
53835383 This function is only valid within function scope.
53845384 </p>
53855385 {#header_close#}
5386 {#header_open|@handle#}
5387 <pre><code class="zig">@handle()</code></pre>
5388 <p>
5389 This function returns a <code>promise->T</code> type, where <code>T</code>
5390 is the return type of the async function in scope.
5391 </p>
5392 <p>
5393 This function is only valid within an async function scope.
5394 </p>
5395 {#header_close#}
53865396 {#header_open|@import#}
53875397 <pre><code class="zig">@import(comptime path: []u8) (namespace)</code></pre>
53885398 <p>
......@@ -7388,7 +7398,7 @@ Defer(body) = ("defer" | "deferror") body
73887398
73897399IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
73907400
7391SuspendExpression(body) = "suspend" option(("|" Symbol "|" body))
7401SuspendExpression(body) = "suspend" option( body )
73927402
73937403IfErrorExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
73947404
src/all_types.hpp+7-1
......@@ -899,7 +899,6 @@ struct AstNodeAwaitExpr {
899899
900900struct AstNodeSuspend {
901901 AstNode *block;
902 AstNode *promise_symbol;
903902};
904903
905904struct AstNodePromiseType {
......@@ -1358,6 +1357,7 @@ enum BuiltinFnId {
13581357 BuiltinFnIdBreakpoint,
13591358 BuiltinFnIdReturnAddress,
13601359 BuiltinFnIdFrameAddress,
1360 BuiltinFnIdHandle,
13611361 BuiltinFnIdEmbedFile,
13621362 BuiltinFnIdCmpxchgWeak,
13631363 BuiltinFnIdCmpxchgStrong,
......@@ -1716,6 +1716,7 @@ struct CodeGen {
17161716 LLVMValueRef coro_save_fn_val;
17171717 LLVMValueRef coro_promise_fn_val;
17181718 LLVMValueRef coro_alloc_helper_fn_val;
1719 LLVMValueRef coro_frame_fn_val;
17191720 LLVMValueRef merge_err_ret_traces_fn_val;
17201721 LLVMValueRef add_error_return_trace_addr_fn_val;
17211722 LLVMValueRef stacksave_fn_val;
......@@ -2076,6 +2077,7 @@ enum IrInstructionId {
20762077 IrInstructionIdBreakpoint,
20772078 IrInstructionIdReturnAddress,
20782079 IrInstructionIdFrameAddress,
2080 IrInstructionIdHandle,
20792081 IrInstructionIdAlignOf,
20802082 IrInstructionIdOverflowOp,
20812083 IrInstructionIdTestErr,
......@@ -2793,6 +2795,10 @@ struct IrInstructionFrameAddress {
27932795 IrInstruction base;
27942796};
27952797
2798struct IrInstructionHandle {
2799 IrInstruction base;
2800};
2801
27962802enum IrOverflowOp {
27972803 IrOverflowOpAdd,
27982804 IrOverflowOpSub,
src/ast_render.cpp-3
......@@ -1112,9 +1112,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
11121112 {
11131113 fprintf(ar->f, "suspend");
11141114 if (node->data.suspend.block != nullptr) {
1115 fprintf(ar->f, " |");
1116 render_node_grouped(ar, node->data.suspend.promise_symbol);
1117 fprintf(ar->f, "| ");
11181115 render_node_grouped(ar, node->data.suspend.block);
11191116 }
11201117 break;
src/codegen.cpp+24
......@@ -4146,6 +4146,26 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable
41464146 return LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
41474147}
41484148
4149static LLVMValueRef get_handle_fn_val(CodeGen *g) {
4150 if (g->coro_frame_fn_val)
4151 return g->coro_frame_fn_val;
4152
4153 LLVMTypeRef fn_type = LLVMFunctionType( LLVMPointerType(LLVMInt8Type(), 0)
4154 , nullptr, 0, false);
4155 Buf *name = buf_sprintf("llvm.coro.frame");
4156 g->coro_frame_fn_val = LLVMAddFunction(g->module, buf_ptr(name), fn_type);
4157 assert(LLVMGetIntrinsicID(g->coro_frame_fn_val));
4158
4159 return g->coro_frame_fn_val;
4160}
4161
4162static LLVMValueRef ir_render_handle(CodeGen *g, IrExecutable *executable,
4163 IrInstructionHandle *instruction)
4164{
4165 LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_promise->type_ref);
4166 return LLVMBuildCall(g->builder, get_handle_fn_val(g), &zero, 0, "");
4167}
4168
41494169static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) {
41504170 TypeTableEntry *int_type = instruction->result_ptr_type;
41514171 assert(int_type->id == TypeTableEntryIdInt);
......@@ -4910,6 +4930,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
49104930 return ir_render_return_address(g, executable, (IrInstructionReturnAddress *)instruction);
49114931 case IrInstructionIdFrameAddress:
49124932 return ir_render_frame_address(g, executable, (IrInstructionFrameAddress *)instruction);
4933 case IrInstructionIdHandle:
4934 return ir_render_handle(g, executable, (IrInstructionHandle *)instruction);
49134935 case IrInstructionIdOverflowOp:
49144936 return ir_render_overflow_op(g, executable, (IrInstructionOverflowOp *)instruction);
49154937 case IrInstructionIdTestErr:
......@@ -6005,6 +6027,7 @@ static void do_code_gen(CodeGen *g) {
60056027 ir_render(g, fn_table_entry);
60066028
60076029 }
6030
60086031 assert(!g->errors.length);
60096032
60106033 if (buf_len(&g->global_asm) != 0) {
......@@ -6344,6 +6367,7 @@ static void define_builtin_fns(CodeGen *g) {
63446367 create_builtin_fn(g, BuiltinFnIdBreakpoint, "breakpoint", 0);
63456368 create_builtin_fn(g, BuiltinFnIdReturnAddress, "returnAddress", 0);
63466369 create_builtin_fn(g, BuiltinFnIdFrameAddress, "frameAddress", 0);
6370 create_builtin_fn(g, BuiltinFnIdHandle, "handle", 0);
63476371 create_builtin_fn(g, BuiltinFnIdMemcpy, "memcpy", 3);
63486372 create_builtin_fn(g, BuiltinFnIdMemset, "memset", 3);
63496373 create_builtin_fn(g, BuiltinFnIdSizeof, "sizeOf", 1);
src/ir.cpp+39-12
......@@ -580,6 +580,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFrameAddress *)
580580 return IrInstructionIdFrameAddress;
581581}
582582
583static constexpr IrInstructionId ir_instruction_id(IrInstructionHandle *) {
584 return IrInstructionIdHandle;
585}
586
583587static constexpr IrInstructionId ir_instruction_id(IrInstructionAlignOf *) {
584588 return IrInstructionIdAlignOf;
585589}
......@@ -2240,6 +2244,17 @@ static IrInstruction *ir_build_frame_address_from(IrBuilder *irb, IrInstruction
22402244 return new_instruction;
22412245}
22422246
2247static IrInstruction *ir_build_handle(IrBuilder *irb, Scope *scope, AstNode *source_node) {
2248 IrInstructionHandle *instruction = ir_build_instruction<IrInstructionHandle>(irb, scope, source_node);
2249 return &instruction->base;
2250}
2251
2252static IrInstruction *ir_build_handle_from(IrBuilder *irb, IrInstruction *old_instruction) {
2253 IrInstruction *new_instruction = ir_build_handle(irb, old_instruction->scope, old_instruction->source_node);
2254 ir_link_new_instruction(new_instruction, old_instruction);
2255 return new_instruction;
2256}
2257
22432258static IrInstruction *ir_build_overflow_op(IrBuilder *irb, Scope *scope, AstNode *source_node,
22442259 IrOverflowOp op, IrInstruction *type_value, IrInstruction *op1, IrInstruction *op2,
22452260 IrInstruction *result_ptr, TypeTableEntry *result_ptr_type)
......@@ -3843,6 +3858,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
38433858 return irb->codegen->invalid_instruction;
38443859 }
38453860
3861 bool is_async = exec_is_async(irb->exec);
3862
38463863 switch (builtin_fn->id) {
38473864 case BuiltinFnIdInvalid:
38483865 zig_unreachable();
......@@ -4475,6 +4492,16 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
44754492 return ir_lval_wrap(irb, scope, ir_build_return_address(irb, scope, node), lval);
44764493 case BuiltinFnIdFrameAddress:
44774494 return ir_lval_wrap(irb, scope, ir_build_frame_address(irb, scope, node), lval);
4495 case BuiltinFnIdHandle:
4496 if (!irb->exec->fn_entry) {
4497 add_node_error(irb->codegen, node, buf_sprintf("@handle() called outside of function definition"));
4498 return irb->codegen->invalid_instruction;
4499 }
4500 if (!is_async) {
4501 add_node_error(irb->codegen, node, buf_sprintf("@handle() in non-async function"));
4502 return irb->codegen->invalid_instruction;
4503 }
4504 return ir_lval_wrap(irb, scope, ir_build_handle(irb, scope, node), lval);
44784505 case BuiltinFnIdAlignOf:
44794506 {
44804507 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -7069,19 +7096,8 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
70697096 if (node->data.suspend.block == nullptr) {
70707097 suspend_code = ir_build_coro_suspend(irb, parent_scope, node, nullptr, const_bool_false);
70717098 } else {
7072 assert(node->data.suspend.promise_symbol != nullptr);
7073 assert(node->data.suspend.promise_symbol->type == NodeTypeSymbol);
7074 Buf *promise_symbol_name = node->data.suspend.promise_symbol->data.symbol_expr.symbol;
70757099 Scope *child_scope;
7076 if (!buf_eql_str(promise_symbol_name, "_")) {
7077 VariableTableEntry *promise_var = ir_create_var(irb, node, parent_scope, promise_symbol_name,
7078 true, true, false, const_bool_false);
7079 ir_build_var_decl(irb, parent_scope, node, promise_var, nullptr, nullptr, irb->exec->coro_handle);
7080 child_scope = promise_var->child_scope;
7081 } else {
7082 child_scope = parent_scope;
7083 }
7084 ScopeSuspend *suspend_scope = create_suspend_scope(node, child_scope);
7100 ScopeSuspend *suspend_scope = create_suspend_scope(node, parent_scope);
70857101 suspend_scope->resume_block = resume_block;
70867102 child_scope = &suspend_scope->base;
70877103 IrInstruction *save_token = ir_build_coro_save(irb, child_scope, node, irb->exec->coro_handle);
......@@ -19007,6 +19023,14 @@ static TypeTableEntry *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrIn
1900719023 return u8_ptr_const;
1900819024}
1900919025
19026static TypeTableEntry *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructionHandle *instruction) {
19027 ir_build_handle_from(&ira->new_irb, &instruction->base);
19028
19029 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
19030 assert(fn_entry != nullptr);
19031 return get_promise_type(ira->codegen, fn_entry->type_entry->data.fn.fn_type_id.return_type);
19032}
19033
1901019034static TypeTableEntry *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
1901119035 IrInstruction *type_value = instruction->type_value->other;
1901219036 if (type_is_invalid(type_value->value.type))
......@@ -20982,6 +21006,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
2098221006 return ir_analyze_instruction_return_address(ira, (IrInstructionReturnAddress *)instruction);
2098321007 case IrInstructionIdFrameAddress:
2098421008 return ir_analyze_instruction_frame_address(ira, (IrInstructionFrameAddress *)instruction);
21009 case IrInstructionIdHandle:
21010 return ir_analyze_instruction_handle(ira, (IrInstructionHandle *)instruction);
2098521011 case IrInstructionIdAlignOf:
2098621012 return ir_analyze_instruction_align_of(ira, (IrInstructionAlignOf *)instruction);
2098721013 case IrInstructionIdOverflowOp:
......@@ -21274,6 +21300,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2127421300 case IrInstructionIdAlignOf:
2127521301 case IrInstructionIdReturnAddress:
2127621302 case IrInstructionIdFrameAddress:
21303 case IrInstructionIdHandle:
2127721304 case IrInstructionIdTestErr:
2127821305 case IrInstructionIdUnwrapErrCode:
2127921306 case IrInstructionIdOptionalWrap:
src/ir_print.cpp+7
......@@ -791,6 +791,10 @@ static void ir_print_frame_address(IrPrint *irp, IrInstructionFrameAddress *inst
791791 fprintf(irp->f, "@frameAddress()");
792792}
793793
794static void ir_print_handle(IrPrint *irp, IrInstructionHandle *instruction) {
795 fprintf(irp->f, "@handle()");
796}
797
794798static void ir_print_return_address(IrPrint *irp, IrInstructionReturnAddress *instruction) {
795799 fprintf(irp->f, "@returnAddress()");
796800}
......@@ -1556,6 +1560,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15561560 case IrInstructionIdFrameAddress:
15571561 ir_print_frame_address(irp, (IrInstructionFrameAddress *)instruction);
15581562 break;
1563 case IrInstructionIdHandle:
1564 ir_print_handle(irp, (IrInstructionHandle *)instruction);
1565 break;
15591566 case IrInstructionIdAlignOf:
15601567 ir_print_align_of(irp, (IrInstructionAlignOf *)instruction);
15611568 break;
src/parser.cpp+9-16
......@@ -648,12 +648,11 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc, size_t *token_index, bool m
648648}
649649
650650/*
651SuspendExpression(body) = "suspend" option(("|" Symbol "|" body))
651SuspendExpression(body) = "suspend" option( body )
652652*/
653653static AstNode *ast_parse_suspend_block(ParseContext *pc, size_t *token_index, bool mandatory) {
654 size_t orig_token_index = *token_index;
655
656654 Token *suspend_token = &pc->tokens->at(*token_index);
655
657656 if (suspend_token->id == TokenIdKeywordSuspend) {
658657 *token_index += 1;
659658 } else if (mandatory) {
......@@ -663,23 +662,18 @@ static AstNode *ast_parse_suspend_block(ParseContext *pc, size_t *token_index, b
663662 return nullptr;
664663 }
665664
666 Token *bar_token = &pc->tokens->at(*token_index);
667 if (bar_token->id == TokenIdBinOr) {
668 *token_index += 1;
665 Token *lbrace = &pc->tokens->at(*token_index);
666 if (lbrace->id == TokenIdLBrace) {
667 AstNode *node = ast_create_node(pc, NodeTypeSuspend, suspend_token);
668 node->data.suspend.block = ast_parse_block(pc, token_index, true);
669 return node;
669670 } else if (mandatory) {
670 ast_expect_token(pc, suspend_token, TokenIdBinOr);
671 ast_expect_token(pc, lbrace, TokenIdLBrace);
671672 zig_unreachable();
672673 } else {
673 *token_index = orig_token_index;
674 *token_index -= 1;
674675 return nullptr;
675676 }
676
677 AstNode *node = ast_create_node(pc, NodeTypeSuspend, suspend_token);
678 node->data.suspend.promise_symbol = ast_parse_symbol(pc, token_index);
679 ast_eat_token(pc, token_index, TokenIdBinOr);
680 node->data.suspend.block = ast_parse_block(pc, token_index, true);
681
682 return node;
683677}
684678
685679/*
......@@ -3134,7 +3128,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
31343128 visit_field(&node->data.await_expr.expr, visit, context);
31353129 break;
31363130 case NodeTypeSuspend:
3137 visit_field(&node->data.suspend.promise_symbol, visit, context);
31383131 visit_field(&node->data.suspend.block, visit, context);
31393132 break;
31403133 }
std/event/channel.zig+4-4
......@@ -71,10 +71,10 @@ pub fn Channel(comptime T: type) type {
7171 /// puts a data item in the channel. The promise completes when the value has been added to the
7272 /// buffer, or in the case of a zero size buffer, when the item has been retrieved by a getter.
7373 pub async fn put(self: *SelfChannel, data: T) void {
74 suspend |handle| {
74 suspend {
7575 var my_tick_node = Loop.NextTickNode{
7676 .next = undefined,
77 .data = handle,
77 .data = @handle(),
7878 };
7979 var queue_node = std.atomic.Queue(PutNode).Node{
8080 .data = PutNode{
......@@ -96,10 +96,10 @@ pub fn Channel(comptime T: type) type {
9696 // TODO integrate this function with named return values
9797 // so we can get rid of this extra result copy
9898 var result: T = undefined;
99 suspend |handle| {
99 suspend {
100100 var my_tick_node = Loop.NextTickNode{
101101 .next = undefined,
102 .data = handle,
102 .data = @handle(),
103103 };
104104 var queue_node = std.atomic.Queue(GetNode).Node{
105105 .data = GetNode{
std/event/future.zig+6-6
......@@ -100,8 +100,8 @@ test "std.event.Future" {
100100}
101101
102102async fn testFuture(loop: *Loop) void {
103 suspend |p| {
104 resume p;
103 suspend {
104 resume @handle();
105105 }
106106 var future = Future(i32).init(loop);
107107
......@@ -115,15 +115,15 @@ async fn testFuture(loop: *Loop) void {
115115}
116116
117117async fn waitOnFuture(future: *Future(i32)) i32 {
118 suspend |p| {
119 resume p;
118 suspend {
119 resume @handle();
120120 }
121121 return (await (async future.get() catch @panic("memory"))).*;
122122}
123123
124124async fn resolveFuture(future: *Future(i32)) void {
125 suspend |p| {
126 resume p;
125 suspend {
126 resume @handle();
127127 }
128128 future.data = 6;
129129 future.resolve();
std/event/group.zig+2-2
......@@ -54,10 +54,10 @@ pub fn Group(comptime ReturnType: type) type {
5454 const S = struct {
5555 async fn asyncFunc(node: **Stack.Node, args2: ...) ReturnType {
5656 // TODO this is a hack to make the memory following be inside the coro frame
57 suspend |p| {
57 suspend {
5858 var my_node: Stack.Node = undefined;
5959 node.* = &my_node;
60 resume p;
60 resume @handle();
6161 }
6262
6363 // TODO this allocation elision should be guaranteed because we await it in
std/event/lock.zig+4-4
......@@ -90,10 +90,10 @@ pub const Lock = struct {
9090 }
9191
9292 pub async fn acquire(self: *Lock) Held {
93 suspend |handle| {
93 suspend {
9494 // TODO explicitly put this memory in the coroutine frame #1194
9595 var my_tick_node = Loop.NextTickNode{
96 .data = handle,
96 .data = @handle(),
9797 .next = undefined,
9898 };
9999
......@@ -141,8 +141,8 @@ test "std.event.Lock" {
141141
142142async fn testLock(loop: *Loop, lock: *Lock) void {
143143 // TODO explicitly put next tick node memory in the coroutine frame #1194
144 suspend |p| {
145 resume p;
144 suspend {
145 resume @handle();
146146 }
147147 const handle1 = async lockRunner(lock) catch @panic("out of memory");
148148 var tick_node1 = Loop.NextTickNode{
std/event/loop.zig+7-7
......@@ -331,11 +331,11 @@ pub const Loop = struct {
331331
332332 pub async fn waitFd(self: *Loop, fd: i32) !void {
333333 defer self.removeFd(fd);
334 suspend |p| {
334 suspend {
335335 // TODO explicitly put this memory in the coroutine frame #1194
336336 var resume_node = ResumeNode{
337337 .id = ResumeNode.Id.Basic,
338 .handle = p,
338 .handle = @handle(),
339339 };
340340 try self.addFd(fd, &resume_node);
341341 }
......@@ -417,11 +417,11 @@ pub const Loop = struct {
417417 pub fn call(self: *Loop, comptime func: var, args: ...) !(promise->@typeOf(func).ReturnType) {
418418 const S = struct {
419419 async fn asyncFunc(loop: *Loop, handle: *promise->@typeOf(func).ReturnType, args2: ...) @typeOf(func).ReturnType {
420 suspend |p| {
421 handle.* = p;
420 suspend {
421 handle.* = @handle();
422422 var my_tick_node = Loop.NextTickNode{
423423 .next = undefined,
424 .data = p,
424 .data = @handle(),
425425 };
426426 loop.onNextTick(&my_tick_node);
427427 }
......@@ -439,10 +439,10 @@ pub const Loop = struct {
439439 /// CPU bound tasks would be waiting in the event loop but never get started because no async I/O
440440 /// is performed.
441441 pub async fn yield(self: *Loop) void {
442 suspend |p| {
442 suspend {
443443 var my_tick_node = Loop.NextTickNode{
444444 .next = undefined,
445 .data = p,
445 .data = @handle(),
446446 };
447447 self.onNextTick(&my_tick_node);
448448 }
std/event/tcp.zig+4-4
......@@ -88,8 +88,8 @@ pub const Server = struct {
8888 },
8989 error.ProcessFdQuotaExceeded => {
9090 errdefer std.os.emfile_promise_queue.remove(&self.waiting_for_emfile_node);
91 suspend |p| {
92 self.waiting_for_emfile_node = PromiseNode.init(p);
91 suspend {
92 self.waiting_for_emfile_node = PromiseNode.init( @handle() );
9393 std.os.emfile_promise_queue.append(&self.waiting_for_emfile_node);
9494 }
9595 continue;
......@@ -141,8 +141,8 @@ test "listen on a port, send bytes, receive bytes" {
141141 (await next_handler) catch |err| {
142142 std.debug.panic("unable to handle connection: {}\n", err);
143143 };
144 suspend |p| {
145 cancel p;
144 suspend {
145 cancel @handle();
146146 }
147147 }
148148 async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: *const std.os.File) !void {
std/zig/ast.zig-12
......@@ -1778,19 +1778,12 @@ pub const Node = struct {
17781778
17791779 pub const Suspend = struct {
17801780 base: Node,
1781 label: ?TokenIndex,
17821781 suspend_token: TokenIndex,
1783 payload: ?*Node,
17841782 body: ?*Node,
17851783
17861784 pub fn iterate(self: *Suspend, index: usize) ?*Node {
17871785 var i = index;
17881786
1789 if (self.payload) |payload| {
1790 if (i < 1) return payload;
1791 i -= 1;
1792 }
1793
17941787 if (self.body) |body| {
17951788 if (i < 1) return body;
17961789 i -= 1;
......@@ -1800,7 +1793,6 @@ pub const Node = struct {
18001793 }
18011794
18021795 pub fn firstToken(self: *Suspend) TokenIndex {
1803 if (self.label) |label| return label;
18041796 return self.suspend_token;
18051797 }
18061798
......@@ -1809,10 +1801,6 @@ pub const Node = struct {
18091801 return body.lastToken();
18101802 }
18111803
1812 if (self.payload) |payload| {
1813 return payload.lastToken();
1814 }
1815
18161804 return self.suspend_token;
18171805 }
18181806 };
std/zig/parse.zig+14-19
......@@ -852,19 +852,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
852852 }) catch unreachable;
853853 continue;
854854 },
855 Token.Id.Keyword_suspend => {
856 const node = try arena.create(ast.Node.Suspend{
857 .base = ast.Node{ .id = ast.Node.Id.Suspend },
858 .label = ctx.label,
859 .suspend_token = token_index,
860 .payload = null,
861 .body = null,
862 });
863 ctx.opt_ctx.store(&node.base);
864 stack.append(State{ .SuspendBody = node }) catch unreachable;
865 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
866 continue;
867 },
868855 Token.Id.Keyword_inline => {
869856 stack.append(State{
870857 .Inline = InlineCtx{
......@@ -1415,10 +1402,21 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
14151402 },
14161403
14171404 State.SuspendBody => |suspend_node| {
1418 if (suspend_node.payload != null) {
1419 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = &suspend_node.body } });
1405 const token = nextToken(&tok_it, &tree);
1406 switch (token.ptr.id) {
1407 Token.Id.Semicolon => {
1408 prevToken(&tok_it, &tree);
1409 continue;
1410 },
1411 Token.Id.LBrace => {
1412 prevToken(&tok_it, &tree);
1413 try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .RequiredNull = &suspend_node.body } });
1414 continue;
1415 },
1416 else => {
1417 ((try tree.errors.addOne())).* = Error{ .InvalidToken = Error.InvalidToken{ .token = token.index } };
1418 },
14201419 }
1421 continue;
14221420 },
14231421 State.AsyncAllocator => |async_node| {
14241422 if (eatToken(&tok_it, &tree, Token.Id.AngleBracketLeft) == null) {
......@@ -3086,15 +3084,12 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: *con
30863084 Token.Id.Keyword_suspend => {
30873085 const node = try arena.create(ast.Node.Suspend{
30883086 .base = ast.Node{ .id = ast.Node.Id.Suspend },
3089 .label = null,
30903087 .suspend_token = token_index,
3091 .payload = null,
30923088 .body = null,
30933089 });
30943090 ctx.store(&node.base);
30953091
30963092 stack.append(State{ .SuspendBody = node }) catch unreachable;
3097 try stack.append(State{ .Payload = OptionalCtx{ .Optional = &node.payload } });
30983093 return true;
30993094 },
31003095 Token.Id.Keyword_if => {
std/zig/parser_test.zig+4-4
......@@ -898,11 +898,11 @@ test "zig fmt: union(enum(u32)) with assigned enum values" {
898898 );
899899}
900900
901test "zig fmt: labeled suspend" {
901test "zig fmt: resume from suspend block" {
902902 try testCanonical(
903903 \\fn foo() void {
904 \\ s: suspend |p| {
905 \\ break :s;
904 \\ suspend {
905 \\ resume @handle();
906906 \\ }
907907 \\}
908908 \\
......@@ -1784,7 +1784,7 @@ test "zig fmt: coroutines" {
17841784 \\ x += 1;
17851785 \\ suspend;
17861786 \\ x += 1;
1787 \\ suspend |p| {}
1787 \\ suspend;
17881788 \\ const p: promise->void = async simpleAsyncFn() catch unreachable;
17891789 \\ await p;
17901790 \\}
std/zig/render.zig+1-15
......@@ -323,21 +323,7 @@ fn renderExpression(
323323 ast.Node.Id.Suspend => {
324324 const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base);
325325
326 if (suspend_node.label) |label| {
327 try renderToken(tree, stream, label, indent, start_col, Space.None);
328 try renderToken(tree, stream, tree.nextToken(label), indent, start_col, Space.Space);
329 }
330
331 if (suspend_node.payload) |payload| {
332 if (suspend_node.body) |body| {
333 try renderToken(tree, stream, suspend_node.suspend_token, indent, start_col, Space.Space);
334 try renderExpression(allocator, stream, tree, indent, start_col, payload, Space.Space);
335 return renderExpression(allocator, stream, tree, indent, start_col, body, space);
336 } else {
337 try renderToken(tree, stream, suspend_node.suspend_token, indent, start_col, Space.Space);
338 return renderExpression(allocator, stream, tree, indent, start_col, payload, space);
339 }
340 } else if (suspend_node.body) |body| {
326 if (suspend_node.body) |body| {
341327 try renderToken(tree, stream, suspend_node.suspend_token, indent, start_col, Space.Space);
342328 return renderExpression(allocator, stream, tree, indent, start_col, body, space);
343329 } else {
test/cases/cancel.zig+2-2
......@@ -85,8 +85,8 @@ async fn b4() void {
8585 defer {
8686 defer_b4 = true;
8787 }
88 suspend |p| {
89 b4_handle = p;
88 suspend {
89 b4_handle = @handle();
9090 }
9191 suspend;
9292}
test/cases/coroutine_await_struct.zig+2-2
......@@ -30,9 +30,9 @@ async fn await_amain() void {
3030}
3131async fn await_another() Foo {
3232 await_seq('c');
33 suspend |p| {
33 suspend {
3434 await_seq('d');
35 await_a_promise = p;
35 await_a_promise = @handle();
3636 }
3737 await_seq('g');
3838 return Foo{ .x = 1234 };
test/cases/coroutines.zig+12-7
......@@ -62,10 +62,15 @@ test "coroutine suspend with block" {
6262var a_promise: promise = undefined;
6363var result = false;
6464async fn testSuspendBlock() void {
65 suspend |p| {
66 comptime assert(@typeOf(p) == promise->void);
67 a_promise = p;
65 suspend {
66 comptime assert(@typeOf(@handle()) == promise->void);
67 a_promise = @handle();
6868 }
69
70 //Test to make sure that @handle() works as advertised (issue #1296)
71 //var our_handle: promise = @handle();
72 assert( a_promise == @handle() );
73
6974 result = true;
7075}
7176
......@@ -93,9 +98,9 @@ async fn await_amain() void {
9398}
9499async fn await_another() i32 {
95100 await_seq('c');
96 suspend |p| {
101 suspend {
97102 await_seq('d');
98 await_a_promise = p;
103 await_a_promise = @handle();
99104 }
100105 await_seq('g');
101106 return 1234;
......@@ -244,8 +249,8 @@ test "break from suspend" {
244249 std.debug.assert(my_result == 2);
245250}
246251async fn testBreakFromSuspend(my_result: *i32) void {
247 suspend |p| {
248 resume p;
252 suspend {
253 resume @handle();
249254 }
250255 my_result.* += 1;
251256 suspend;
test/compile_errors.zig+23-2
......@@ -1,6 +1,27 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "@handle() called outside of function definition",
6 \\var handle_undef: promise = undefined;
7 \\var handle_dummy: promise = @handle();
8 \\export fn entry() bool {
9 \\ return handle_undef == handle_dummy;
10 \\}
11 ,
12 ".tmp_source.zig:2:29: error: @handle() called outside of function definition",
13 );
14
15 cases.add(
16 "@handle() in non-async function",
17 \\export fn entry() bool {
18 \\ var handle_undef: promise = undefined;
19 \\ return handle_undef == @handle();
20 \\}
21 ,
22 ".tmp_source.zig:3:28: error: @handle() in non-async function",
23 );
24
425 cases.add(
526 "while loop body expression ignored",
627 \\fn returns() usize {
......@@ -367,8 +388,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
367388 \\}
368389 \\
369390 \\async fn foo() void {
370 \\ suspend |p| {
371 \\ suspend |p1| {
391 \\ suspend {
392 \\ suspend {
372393 \\ }
373394 \\ }
374395 \\}