authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 11:27:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 11:38:41-04:00
log13c584d325d042879c8c56a3c41ffbf99a3346c0
tree8dbe57f0ef8a1da2b9c6db9471a82ebe36aa90aa
parentcba3b8291a18ee16cda2b453bb2bcd4279fa8b98
signaturelock-open Commit is signed but in an unrecognized format.

add compile error for casting const frame to anyframe

See #3063

8 files changed, 39 insertions(+), 20 deletions(-)

src/ir.cpp+1
...@@ -12112,6 +12112,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -12112,6 +12112,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1211212112
12113 // *@Frame(func) to anyframe->T or anyframe12113 // *@Frame(func) to anyframe->T or anyframe
12114 if (actual_type->id == ZigTypeIdPointer && actual_type->data.pointer.ptr_len == PtrLenSingle &&12114 if (actual_type->id == ZigTypeIdPointer && actual_type->data.pointer.ptr_len == PtrLenSingle &&
12115 !actual_type->data.pointer.is_const &&
12115 actual_type->data.pointer.child_type->id == ZigTypeIdFnFrame && wanted_type->id == ZigTypeIdAnyFrame)12116 actual_type->data.pointer.child_type->id == ZigTypeIdFnFrame && wanted_type->id == ZigTypeIdAnyFrame)
12116 {12117 {
12117 bool ok = true;12118 bool ok = true;
std/event/channel.zig+1-1
...@@ -331,7 +331,7 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {...@@ -331,7 +331,7 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {
331 const value3 = channel.getOrNull();331 const value3 = channel.getOrNull();
332 testing.expect(value3 == null);332 testing.expect(value3 == null);
333333
334 const last_put = async testPut(channel, 4444);334 var last_put = async testPut(channel, 4444);
335 const value4 = channel.getOrNull();335 const value4 = channel.getOrNull();
336 testing.expect(value4.? == 4444);336 testing.expect(value4.? == 4444);
337 await last_put;337 await last_put;
std/event/future.zig+3-3
...@@ -100,9 +100,9 @@ test "std.event.Future" {...@@ -100,9 +100,9 @@ test "std.event.Future" {
100async fn testFuture(loop: *Loop) void {100async fn testFuture(loop: *Loop) void {
101 var future = Future(i32).init(loop);101 var future = Future(i32).init(loop);
102102
103 const a = async waitOnFuture(&future);103 var a = async waitOnFuture(&future);
104 const b = async waitOnFuture(&future);104 var b = async waitOnFuture(&future);
105 const c = async resolveFuture(&future);105 var c = async resolveFuture(&future);
106106
107 // TODO make this work:107 // TODO make this work:
108 //const result = (await a) + (await b);108 //const result = (await a) + (await b);
std/event/lock.zig+5-5
...@@ -135,7 +135,7 @@ test "std.event.Lock" {...@@ -135,7 +135,7 @@ test "std.event.Lock" {
135}135}
136136
137async fn testLock(loop: *Loop, lock: *Lock) void {137async fn testLock(loop: *Loop, lock: *Lock) void {
138 const handle1 = async lockRunner(lock);138 var handle1 = async lockRunner(lock);
139 var tick_node1 = Loop.NextTickNode{139 var tick_node1 = Loop.NextTickNode{
140 .prev = undefined,140 .prev = undefined,
141 .next = undefined,141 .next = undefined,
...@@ -143,7 +143,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {...@@ -143,7 +143,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {
143 };143 };
144 loop.onNextTick(&tick_node1);144 loop.onNextTick(&tick_node1);
145145
146 const handle2 = async lockRunner(lock);146 var handle2 = async lockRunner(lock);
147 var tick_node2 = Loop.NextTickNode{147 var tick_node2 = Loop.NextTickNode{
148 .prev = undefined,148 .prev = undefined,
149 .next = undefined,149 .next = undefined,
...@@ -151,7 +151,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {...@@ -151,7 +151,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {
151 };151 };
152 loop.onNextTick(&tick_node2);152 loop.onNextTick(&tick_node2);
153153
154 const handle3 = async lockRunner(lock);154 var handle3 = async lockRunner(lock);
155 var tick_node3 = Loop.NextTickNode{155 var tick_node3 = Loop.NextTickNode{
156 .prev = undefined,156 .prev = undefined,
157 .next = undefined,157 .next = undefined,
...@@ -172,8 +172,8 @@ async fn lockRunner(lock: *Lock) void {...@@ -172,8 +172,8 @@ async fn lockRunner(lock: *Lock) void {
172172
173 var i: usize = 0;173 var i: usize = 0;
174 while (i < shared_test_data.len) : (i += 1) {174 while (i < shared_test_data.len) : (i += 1) {
175 const lock_promise = async lock.acquire();175 var lock_frame = async lock.acquire();
176 const handle = await lock_promise;176 const handle = await lock_frame;
177 defer handle.release();177 defer handle.release();
178178
179 shared_test_index = 0;179 shared_test_index = 0;
std/event/loop.zig+2-2
...@@ -900,8 +900,8 @@ test "std.event.Loop - call" {...@@ -900,8 +900,8 @@ test "std.event.Loop - call" {
900 defer loop.deinit();900 defer loop.deinit();
901901
902 var did_it = false;902 var did_it = false;
903 const handle = async Loop.call(testEventLoop);903 var handle = async Loop.call(testEventLoop);
904 const handle2 = async Loop.call(testEventLoop2, &handle, &did_it);904 var handle2 = async Loop.call(testEventLoop2, &handle, &did_it);
905905
906 loop.run();906 loop.run();
907907
test/compile_errors.zig+18
...@@ -2,6 +2,24 @@ const tests = @import("tests.zig");...@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "const frame cast to anyframe",
7 \\export fn a() void {
8 \\ const f = async func();
9 \\ resume f;
10 \\}
11 \\export fn b() void {
12 \\ const f = async func();
13 \\ var x: anyframe = &f;
14 \\}
15 \\fn func() void {
16 \\ suspend;
17 \\}
18 ,
19 "tmp.zig:3:12: error: expected type 'anyframe', found '*const @Frame(func)'",
20 "tmp.zig:7:24: error: expected type 'anyframe', found '*const @Frame(func)'",
21 );
22
5 cases.add(23 cases.add(
6 "prevent bad implicit casting of anyframe types",24 "prevent bad implicit casting of anyframe types",
7 \\export fn a() void {25 \\export fn a() void {
test/stage1/behavior/async_fn.zig+7-7
...@@ -6,7 +6,7 @@ const expectEqual = std.testing.expectEqual;...@@ -6,7 +6,7 @@ const expectEqual = std.testing.expectEqual;
6var global_x: i32 = 1;6var global_x: i32 = 1;
77
8test "simple coroutine suspend and resume" {8test "simple coroutine suspend and resume" {
9 const frame = async simpleAsyncFn();9 var frame = async simpleAsyncFn();
10 expect(global_x == 2);10 expect(global_x == 2);
11 resume frame;11 resume frame;
12 expect(global_x == 3);12 expect(global_x == 3);
...@@ -25,7 +25,7 @@ fn simpleAsyncFn() void {...@@ -25,7 +25,7 @@ fn simpleAsyncFn() void {
25var global_y: i32 = 1;25var global_y: i32 = 1;
2626
27test "pass parameter to coroutine" {27test "pass parameter to coroutine" {
28 const p = async simpleAsyncFnWithArg(2);28 var p = async simpleAsyncFnWithArg(2);
29 expect(global_y == 3);29 expect(global_y == 3);
30 resume p;30 resume p;
31 expect(global_y == 5);31 expect(global_y == 5);
...@@ -60,7 +60,7 @@ test "local variable in async function" {...@@ -60,7 +60,7 @@ test "local variable in async function" {
6060
61 fn doTheTest() void {61 fn doTheTest() void {
62 expect(x == 0);62 expect(x == 0);
63 const p = async add(1, 2);63 var p = async add(1, 2);
64 expect(x == 0);64 expect(x == 0);
65 resume p;65 resume p;
66 expect(x == 0);66 expect(x == 0);
...@@ -201,7 +201,7 @@ var await_final_result: i32 = 0;...@@ -201,7 +201,7 @@ var await_final_result: i32 = 0;
201201
202test "coroutine await" {202test "coroutine await" {
203 await_seq('a');203 await_seq('a');
204 const p = async await_amain();204 var p = async await_amain();
205 await_seq('f');205 await_seq('f');
206 resume await_a_promise;206 resume await_a_promise;
207 await_seq('i');207 await_seq('i');
...@@ -210,7 +210,7 @@ test "coroutine await" {...@@ -210,7 +210,7 @@ test "coroutine await" {
210}210}
211async fn await_amain() void {211async fn await_amain() void {
212 await_seq('b');212 await_seq('b');
213 const p = async await_another();213 var p = async await_another();
214 await_seq('e');214 await_seq('e');
215 await_final_result = await p;215 await_final_result = await p;
216 await_seq('h');216 await_seq('h');
...@@ -237,14 +237,14 @@ var early_final_result: i32 = 0;...@@ -237,14 +237,14 @@ var early_final_result: i32 = 0;
237237
238test "coroutine await early return" {238test "coroutine await early return" {
239 early_seq('a');239 early_seq('a');
240 const p = async early_amain();240 var p = async early_amain();
241 early_seq('f');241 early_seq('f');
242 expect(early_final_result == 1234);242 expect(early_final_result == 1234);
243 expect(std.mem.eql(u8, early_points, "abcdef"));243 expect(std.mem.eql(u8, early_points, "abcdef"));
244}244}
245async fn early_amain() void {245async fn early_amain() void {
246 early_seq('b');246 early_seq('b');
247 const p = async early_another();247 var p = async early_another();
248 early_seq('d');248 early_seq('d');
249 early_final_result = await p;249 early_final_result = await p;
250 early_seq('e');250 early_seq('e');
test/stage1/behavior/await_struct.zig+2-2
...@@ -11,7 +11,7 @@ var await_final_result = Foo{ .x = 0 };...@@ -11,7 +11,7 @@ var await_final_result = Foo{ .x = 0 };
1111
12test "coroutine await struct" {12test "coroutine await struct" {
13 await_seq('a');13 await_seq('a');
14 const p = async await_amain();14 var p = async await_amain();
15 await_seq('f');15 await_seq('f');
16 resume await_a_promise;16 resume await_a_promise;
17 await_seq('i');17 await_seq('i');
...@@ -20,7 +20,7 @@ test "coroutine await struct" {...@@ -20,7 +20,7 @@ test "coroutine await struct" {
20}20}
21async fn await_amain() void {21async fn await_amain() void {
22 await_seq('b');22 await_seq('b');
23 const p = async await_another();23 var p = async await_another();
24 await_seq('e');24 await_seq('e');
25 await_final_result = await p;25 await_final_result = await p;
26 await_seq('h');26 await_seq('h');