authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-03 01:06:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-03 01:06:14-04:00
log24d78177eec4d8fc3aa8ca99dd50788e38f9f8b6
tree14f0b5e36cb4ec2bc68717c6368afeb06a1b857e
parent0920bb087279bdfa317fc88cef877d40ece18239
signature Commit is signed but in an unrecognized format.

add compile error for async call of function pointer


4 files changed, 21 insertions(+), 6 deletions(-)

BRANCH_TODO+1-1
......@@ -1,4 +1,3 @@
1 * struct types as the return type of an async function. make sure it works with return result locations.
21 * compile error for error: expected anyframe->T, found 'anyframe'
32 * compile error for error: expected anyframe->T, found 'i32'
43 * await of a non async function
......@@ -19,3 +18,4 @@
1918 * make resuming inside a suspend block, with nothing after it, a must-tail call.
2019 * make sure there are safety tests for all the new safety features (search the new PanicFnId enum values)
2120 * error return tracing
21 * compile error for casting a function to a non-async function pointer, but then later it gets inferred to be an async function
src/ir.cpp+4-1
......@@ -14819,7 +14819,10 @@ static IrInstruction *ir_analyze_instruction_reset_result(IrAnalyze *ira, IrInst
1481914819static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc *call_instruction, ZigFn *fn_entry,
1482014820 ZigType *fn_type, IrInstruction *fn_ref, IrInstruction **casted_args, size_t arg_count)
1482114821{
14822 ir_assert(fn_entry != nullptr, &call_instruction->base);
14822 if (fn_entry == nullptr) {
14823 ir_add_error(ira, fn_ref, buf_sprintf("function is not comptime-known; @asyncCall required"));
14824 return ira->codegen->invalid_instruction;
14825 }
1482314826
1482414827 ZigType *frame_type = get_coro_frame_type(ira->codegen, fn_entry);
1482514828 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
test/compile_errors.zig+12
......@@ -2,6 +2,18 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "runtime-known function called with async keyword",
7 \\export fn entry() void {
8 \\ var ptr = afunc;
9 \\ _ = async ptr();
10 \\}
11 \\
12 \\async fn afunc() void { }
13 ,
14 "tmp.zig:3:15: error: function is not comptime-known; @asyncCall required",
15 );
16
517 cases.add(
618 "function with ccc indirectly calling async function",
719 \\export fn entry() void {
test/stage1/behavior/coroutines.zig+4-4
......@@ -263,15 +263,15 @@ test "async function with dot syntax" {
263263//test "async fn pointer in a struct field" {
264264// var data: i32 = 1;
265265// const Foo = struct {
266// bar: async<*std.mem.Allocator> fn (*i32) void,
266// bar: async fn (*i32) void,
267267// };
268268// var foo = Foo{ .bar = simpleAsyncFn2 };
269// const p = (async<allocator> foo.bar(&data)) catch unreachable;
269// const p = async foo.bar(&data);
270270// expect(data == 2);
271// cancel p;
271// resume p;
272272// expect(data == 4);
273273//}
274//async<*std.mem.Allocator> fn simpleAsyncFn2(y: *i32) void {
274//async fn simpleAsyncFn2(y: *i32) void {
275275// defer y.* += 2;
276276// y.* += 1;
277277// suspend;