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
1211212112
1211312113 // *@Frame(func) to anyframe->T or anyframe
1211412114 if (actual_type->id == ZigTypeIdPointer && actual_type->data.pointer.ptr_len == PtrLenSingle &&
12115 !actual_type->data.pointer.is_const &&
1211512116 actual_type->data.pointer.child_type->id == ZigTypeIdFnFrame && wanted_type->id == ZigTypeIdAnyFrame)
1211612117 {
1211712118 bool ok = true;
std/event/channel.zig+1-1
......@@ -331,7 +331,7 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void {
331331 const value3 = channel.getOrNull();
332332 testing.expect(value3 == null);
333333
334 const last_put = async testPut(channel, 4444);
334 var last_put = async testPut(channel, 4444);
335335 const value4 = channel.getOrNull();
336336 testing.expect(value4.? == 4444);
337337 await last_put;
std/event/future.zig+3-3
......@@ -100,9 +100,9 @@ test "std.event.Future" {
100100async fn testFuture(loop: *Loop) void {
101101 var future = Future(i32).init(loop);
102102
103 const a = async waitOnFuture(&future);
104 const b = async waitOnFuture(&future);
105 const c = async resolveFuture(&future);
103 var a = async waitOnFuture(&future);
104 var b = async waitOnFuture(&future);
105 var c = async resolveFuture(&future);
106106
107107 // TODO make this work:
108108 //const result = (await a) + (await b);
std/event/lock.zig+5-5
......@@ -135,7 +135,7 @@ test "std.event.Lock" {
135135}
136136
137137async fn testLock(loop: *Loop, lock: *Lock) void {
138 const handle1 = async lockRunner(lock);
138 var handle1 = async lockRunner(lock);
139139 var tick_node1 = Loop.NextTickNode{
140140 .prev = undefined,
141141 .next = undefined,
......@@ -143,7 +143,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {
143143 };
144144 loop.onNextTick(&tick_node1);
145145
146 const handle2 = async lockRunner(lock);
146 var handle2 = async lockRunner(lock);
147147 var tick_node2 = Loop.NextTickNode{
148148 .prev = undefined,
149149 .next = undefined,
......@@ -151,7 +151,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void {
151151 };
152152 loop.onNextTick(&tick_node2);
153153
154 const handle3 = async lockRunner(lock);
154 var handle3 = async lockRunner(lock);
155155 var tick_node3 = Loop.NextTickNode{
156156 .prev = undefined,
157157 .next = undefined,
......@@ -172,8 +172,8 @@ async fn lockRunner(lock: *Lock) void {
172172
173173 var i: usize = 0;
174174 while (i < shared_test_data.len) : (i += 1) {
175 const lock_promise = async lock.acquire();
176 const handle = await lock_promise;
175 var lock_frame = async lock.acquire();
176 const handle = await lock_frame;
177177 defer handle.release();
178178
179179 shared_test_index = 0;
std/event/loop.zig+2-2
......@@ -900,8 +900,8 @@ test "std.event.Loop - call" {
900900 defer loop.deinit();
901901
902902 var did_it = false;
903 const handle = async Loop.call(testEventLoop);
904 const handle2 = async Loop.call(testEventLoop2, &handle, &did_it);
903 var handle = async Loop.call(testEventLoop);
904 var handle2 = async Loop.call(testEventLoop2, &handle, &did_it);
905905
906906 loop.run();
907907
test/compile_errors.zig+18
......@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub 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
523 cases.add(
624 "prevent bad implicit casting of anyframe types",
725 \\export fn a() void {
test/stage1/behavior/async_fn.zig+7-7
......@@ -6,7 +6,7 @@ const expectEqual = std.testing.expectEqual;
66var global_x: i32 = 1;
77
88test "simple coroutine suspend and resume" {
9 const frame = async simpleAsyncFn();
9 var frame = async simpleAsyncFn();
1010 expect(global_x == 2);
1111 resume frame;
1212 expect(global_x == 3);
......@@ -25,7 +25,7 @@ fn simpleAsyncFn() void {
2525var global_y: i32 = 1;
2626
2727test "pass parameter to coroutine" {
28 const p = async simpleAsyncFnWithArg(2);
28 var p = async simpleAsyncFnWithArg(2);
2929 expect(global_y == 3);
3030 resume p;
3131 expect(global_y == 5);
......@@ -60,7 +60,7 @@ test "local variable in async function" {
6060
6161 fn doTheTest() void {
6262 expect(x == 0);
63 const p = async add(1, 2);
63 var p = async add(1, 2);
6464 expect(x == 0);
6565 resume p;
6666 expect(x == 0);
......@@ -201,7 +201,7 @@ var await_final_result: i32 = 0;
201201
202202test "coroutine await" {
203203 await_seq('a');
204 const p = async await_amain();
204 var p = async await_amain();
205205 await_seq('f');
206206 resume await_a_promise;
207207 await_seq('i');
......@@ -210,7 +210,7 @@ test "coroutine await" {
210210}
211211async fn await_amain() void {
212212 await_seq('b');
213 const p = async await_another();
213 var p = async await_another();
214214 await_seq('e');
215215 await_final_result = await p;
216216 await_seq('h');
......@@ -237,14 +237,14 @@ var early_final_result: i32 = 0;
237237
238238test "coroutine await early return" {
239239 early_seq('a');
240 const p = async early_amain();
240 var p = async early_amain();
241241 early_seq('f');
242242 expect(early_final_result == 1234);
243243 expect(std.mem.eql(u8, early_points, "abcdef"));
244244}
245245async fn early_amain() void {
246246 early_seq('b');
247 const p = async early_another();
247 var p = async early_another();
248248 early_seq('d');
249249 early_final_result = await p;
250250 early_seq('e');
test/stage1/behavior/await_struct.zig+2-2
......@@ -11,7 +11,7 @@ var await_final_result = Foo{ .x = 0 };
1111
1212test "coroutine await struct" {
1313 await_seq('a');
14 const p = async await_amain();
14 var p = async await_amain();
1515 await_seq('f');
1616 resume await_a_promise;
1717 await_seq('i');
......@@ -20,7 +20,7 @@ test "coroutine await struct" {
2020}
2121async fn await_amain() void {
2222 await_seq('b');
23 const p = async await_another();
23 var p = async await_another();
2424 await_seq('e');
2525 await_final_result = await p;
2626 await_seq('h');