From 9a0a378e2f957f6acf5fbb37818d1cc0e3a38d33 Mon Sep 17 00:00:00 2001 From: Benjamin Feng Date: Wed, 29 Jan 2020 00:34:46 -0600 Subject: [PATCH] Add test cases for suspend in while loops --- test/stage1/behavior/async_fn.zig | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/stage1/behavior/async_fn.zig b/test/stage1/behavior/async_fn.zig index 1b21ba7eff..87e7fca383 100644 --- a/test/stage1/behavior/async_fn.zig +++ b/test/stage1/behavior/async_fn.zig @@ -1182,6 +1182,42 @@ test "suspend in for loop" { S.doTheTest(); } +test "suspend in while loop" { + const S = struct { + var global_frame: ?anyframe = null; + + fn doTheTest() void { + _ = async atest(); + while (global_frame) |f| resume f; + } + + fn atest() void { + expect(optional(6) == 6); + expect(errunion(6) == 6); + } + fn optional(stuff: ?u32) u32 { + global_frame = @frame(); + defer global_frame = null; + while (stuff) |val| { + suspend; + return val; + } + return 0; + } + fn errunion(stuff: anyerror!u32) u32 { + global_frame = @frame(); + defer global_frame = null; + while (stuff) |val| { + suspend; + return val; + } else |err| { + return 0; + } + } + }; + S.doTheTest(); +} + test "correctly spill when returning the error union result of another async fn" { const S = struct { var global_frame: anyframe = undefined;