authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-09 12:25:57-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-09 15:27:26-05:00
logf205d23e650019dd66120cf122ffb449267f619b
tree1d7ab12881bcf381ab46dfdf21653fc690ee44fc
parent69b587c1d389808e55846cadd8eec5b9fd4bc64a
signaturelock-open Commit is signed but in an unrecognized format.

implement async function call with `@call`

this removes the last usage of var args in zig std lib

9 files changed, 57 insertions(+), 29 deletions(-)

doc/langref.html.in+3
......@@ -6880,6 +6880,9 @@ pub const CallOptions = struct {
68806880 /// Equivalent to function call syntax.
68816881 auto,
68826882
6883 /// Equivalent to async keyword used with function call syntax.
6884 async_kw,
6885
68836886 /// Prevents tail call optimization. This guarantees that the return
68846887 /// address will point to the callsite, as opposed to the callsite's
68856888 /// callsite. If the call is otherwise required to be tail-called
lib/std/builtin.zig+3
......@@ -382,6 +382,9 @@ pub const CallOptions = struct {
382382 /// Equivalent to function call syntax.
383383 auto,
384384
385 /// Equivalent to async keyword used with function call syntax.
386 async_kw,
387
385388 /// Prevents tail call optimization. This guarantees that the return
386389 /// address will point to the callsite, as opposed to the callsite's
387390 /// callsite. If the call is otherwise required to be tail-called
lib/std/event/group.zig+6-3
......@@ -60,16 +60,19 @@ pub fn Group(comptime ReturnType: type) type {
6060 /// allocated by the group and freed by `wait`.
6161 /// `func` must be async and have return type `ReturnType`.
6262 /// Thread-safe.
63 pub fn call(self: *Self, comptime func: var, args: ...) error{OutOfMemory}!void {
64 var frame = try self.allocator.create(@Frame(func));
63 pub fn call(self: *Self, comptime func: var, args: var) error{OutOfMemory}!void {
64 var frame = try self.allocator.create(@typeOf(@call(.{ .modifier = .async_kw }, func, args)));
65 errdefer self.allocator.destroy(frame);
6566 const node = try self.allocator.create(AllocStack.Node);
67 errdefer self.allocator.destroy(node);
6668 node.* = AllocStack.Node{
6769 .next = undefined,
6870 .data = Node{
69 .handle = @asyncCall(frame, {}, func, args),
71 .handle = frame,
7072 .bytes = std.mem.asBytes(frame),
7173 },
7274 };
75 frame.* = @call(.{ .modifier = .async_kw }, func, args);
7376 self.alloc_stack.push(node);
7477 }
7578
src-self-hosted/compilation.zig+11-11
......@@ -778,7 +778,7 @@ pub const Compilation = struct {
778778 continue;
779779 };
780780 const root_scope = ev.data;
781 group.call(rebuildFile, self, root_scope) catch |err| {
781 group.call(rebuildFile, .{ self, root_scope }) catch |err| {
782782 build_result = err;
783783 continue;
784784 };
......@@ -787,7 +787,7 @@ pub const Compilation = struct {
787787 while (self.fs_watch.channel.getOrNull()) |ev_or_err| {
788788 if (ev_or_err) |ev| {
789789 const root_scope = ev.data;
790 group.call(rebuildFile, self, root_scope) catch |err| {
790 group.call(rebuildFile, .{ self, root_scope }) catch |err| {
791791 build_result = err;
792792 continue;
793793 };
......@@ -868,7 +868,7 @@ pub const Compilation = struct {
868868
869869 // TODO connect existing comptime decls to updated source files
870870
871 try self.prelink_group.call(addCompTimeBlock, self, tree_scope, &decl_scope.base, comptime_node);
871 try self.prelink_group.call(addCompTimeBlock, .{ self, tree_scope, &decl_scope.base, comptime_node });
872872 },
873873 .VarDecl => @panic("TODO"),
874874 .FnProto => {
......@@ -921,7 +921,7 @@ pub const Compilation = struct {
921921 tree_scope.base.ref();
922922 errdefer self.gpa().destroy(fn_decl);
923923
924 try group.call(addTopLevelDecl, self, &fn_decl.base, locked_table);
924 try group.call(addTopLevelDecl, .{ self, &fn_decl.base, locked_table });
925925 }
926926 },
927927 .TestDecl => @panic("TODO"),
......@@ -1042,8 +1042,8 @@ pub const Compilation = struct {
10421042 const is_export = decl.isExported(decl.tree_scope.tree);
10431043
10441044 if (is_export) {
1045 try self.prelink_group.call(verifyUniqueSymbol, self, decl);
1046 try self.prelink_group.call(resolveDecl, self, decl);
1045 try self.prelink_group.call(verifyUniqueSymbol, .{ self, decl });
1046 try self.prelink_group.call(resolveDecl, .{ self, decl });
10471047 }
10481048
10491049 const gop = try locked_table.getOrPut(decl.name);
......@@ -1062,7 +1062,7 @@ pub const Compilation = struct {
10621062 const msg = try Msg.createFromScope(self, tree_scope, span, text);
10631063 errdefer msg.destroy();
10641064
1065 try self.prelink_group.call(addCompileErrorAsync, self, msg);
1065 try self.prelink_group.call(addCompileErrorAsync, .{ self, msg });
10661066 }
10671067
10681068 fn addCompileErrorCli(self: *Compilation, realpath: []const u8, comptime fmt: []const u8, args: var) !void {
......@@ -1072,7 +1072,7 @@ pub const Compilation = struct {
10721072 const msg = try Msg.createFromCli(self, realpath, text);
10731073 errdefer msg.destroy();
10741074
1075 try self.prelink_group.call(addCompileErrorAsync, self, msg);
1075 try self.prelink_group.call(addCompileErrorAsync, .{ self, msg });
10761076 }
10771077
10781078 async fn addCompileErrorAsync(
......@@ -1131,7 +1131,7 @@ pub const Compilation = struct {
11311131
11321132 // get a head start on looking for the native libc
11331133 if (self.target == Target.Native and self.override_libc == null) {
1134 try self.deinit_group.call(startFindingNativeLibC, self);
1134 try self.deinit_group.call(startFindingNativeLibC, .{self});
11351135 }
11361136 }
11371137 return link_lib;
......@@ -1339,8 +1339,8 @@ fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
13391339
13401340 // Kick off rendering to LLVM module, but it doesn't block the fn decl
13411341 // analysis from being complete.
1342 try comp.prelink_group.call(codegen.renderToLlvm, comp, fn_val, analyzed_code);
1343 try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
1342 try comp.prelink_group.call(codegen.renderToLlvm, .{ comp, fn_val, analyzed_code });
1343 try comp.prelink_group.call(addFnToLinkSet, .{ comp, fn_val });
13441344}
13451345
13461346async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) Compilation.BuildError!void {
src-self-hosted/libc_installation.zig+8-8
......@@ -158,9 +158,9 @@ pub const LibCInstallation = struct {
158158 if (sdk.msvc_lib_dir_ptr != 0) {
159159 self.msvc_lib_dir = try std.mem.dupe(allocator, u8, sdk.msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]);
160160 }
161 try group.call(findNativeKernel32LibDir, allocator, self, sdk);
162 try group.call(findNativeIncludeDirWindows, self, allocator, sdk);
163 try group.call(findNativeLibDirWindows, self, allocator, sdk);
161 try group.call(findNativeKernel32LibDir, .{ allocator, self, sdk });
162 try group.call(findNativeIncludeDirWindows, .{ self, allocator, sdk });
163 try group.call(findNativeLibDirWindows, .{ self, allocator, sdk });
164164 },
165165 c.ZigFindWindowsSdkError.OutOfMemory => return error.OutOfMemory,
166166 c.ZigFindWindowsSdkError.NotFound => return error.NotFound,
......@@ -168,10 +168,10 @@ pub const LibCInstallation = struct {
168168 }
169169 },
170170 .linux => {
171 try group.call(findNativeIncludeDirLinux, self, allocator);
172 try group.call(findNativeLibDirLinux, self, allocator);
173 try group.call(findNativeStaticLibDir, self, allocator);
174 try group.call(findNativeDynamicLinker, self, allocator);
171 try group.call(findNativeIncludeDirLinux, .{ self, allocator });
172 try group.call(findNativeLibDirLinux, .{ self, allocator });
173 try group.call(findNativeStaticLibDir, .{ self, allocator });
174 try group.call(findNativeDynamicLinker, .{ self, allocator });
175175 },
176176 .macosx, .freebsd, .netbsd => {
177177 self.include_dir = try std.mem.dupe(allocator, u8, "/usr/include");
......@@ -322,7 +322,7 @@ pub const LibCInstallation = struct {
322322 var group = event.Group(FindError!void).init(allocator);
323323 errdefer group.wait() catch {};
324324 for (dyn_tests) |*dyn_test| {
325 try group.call(testNativeDynamicLinker, self, allocator, dyn_test);
325 try group.call(testNativeDynamicLinker, .{ self, allocator, dyn_test });
326326 }
327327 try group.wait();
328328 for (dyn_tests) |*dyn_test| {
src-self-hosted/main.zig+2-2
......@@ -654,7 +654,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
654654
655655 var group = event.Group(FmtError!void).init(allocator);
656656 for (flags.positionals.toSliceConst()) |file_path| {
657 try group.call(fmtPath, &fmt, file_path, check_mode);
657 try group.call(fmtPath, .{ &fmt, file_path, check_mode });
658658 }
659659 try group.wait();
660660 if (fmt.any_error) {
......@@ -710,7 +710,7 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
710710 if (entry.kind == .Directory or mem.endsWith(u8, entry.name, ".zig")) {
711711 const full_path = try fs.path.join(fmt.allocator, &[_][]const u8{ file_path, entry.name });
712712 @panic("TODO https://github.com/ziglang/zig/issues/3777");
713 // try group.call(fmtPath, fmt, full_path, check_mode);
713 // try group.call(fmtPath, .{fmt, full_path, check_mode});
714714 }
715715 }
716716 return group.wait();
src/all_types.hpp+1-1
......@@ -782,6 +782,7 @@ struct AstNodeUnwrapOptional {
782782// Must be synchronized with std.builtin.CallOptions.Modifier
783783enum CallModifier {
784784 CallModifierNone,
785 CallModifierAsync,
785786 CallModifierNeverTail,
786787 CallModifierNeverInline,
787788 CallModifierNoAsync,
......@@ -791,7 +792,6 @@ enum CallModifier {
791792
792793 // These are additional tags in the compiler, but not exposed in the std lib.
793794 CallModifierBuiltin,
794 CallModifierAsync,
795795};
796796
797797struct AstNodeFnCallExpr {
src/ir.cpp+1-4
......@@ -18330,10 +18330,7 @@ static IrInstruction *ir_analyze_call_extra(IrAnalyze *ira, IrInstruction *sourc
1833018330 if (modifier_val == nullptr)
1833118331 return ira->codegen->invalid_instruction;
1833218332 CallModifier modifier = (CallModifier)bigint_as_u32(&modifier_val->data.x_enum_tag);
18333 if (modifier == CallModifierAsync) {
18334 ir_add_error(ira, source_instr, buf_sprintf("TODO: @call with async modifier"));
18335 return ira->codegen->invalid_instruction;
18336 }
18333
1833718334 if (ir_should_inline(ira->new_irb.exec, source_instr->scope)) {
1833818335 switch (modifier) {
1833918336 case CallModifierBuiltin:
test/stage1/behavior/async_fn.zig+22
......@@ -1271,3 +1271,25 @@ test "spill target expr in a for loop, with a var decl in the loop body" {
12711271 resume S.global_frame;
12721272 resume S.global_frame;
12731273}
1274
1275test "async call with @call" {
1276 const S = struct {
1277 var global_frame: anyframe = undefined;
1278 fn doTheTest() void {
1279 _ = @call(.{ .modifier = .async_kw }, atest, .{});
1280 resume global_frame;
1281 }
1282 fn atest() void {
1283 var frame = @call(.{ .modifier = .async_kw }, afoo, .{});
1284 const res = await frame;
1285 expect(res == 42);
1286 }
1287 fn afoo() i32 {
1288 suspend {
1289 global_frame = @frame();
1290 }
1291 return 42;
1292 }
1293 };
1294 S.doTheTest();
1295}