authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 00:34:46-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-01-29 10:01:51-06:00
log9a0a378e2f957f6acf5fbb37818d1cc0e3a38d33
tree27cfbbaa86298ae5130041f4f166c395c28cb1e7
parenta4ac7980b4a37c3d9d36b63685c8a5867d4849f5

Add test cases for suspend in while loops


1 files changed, 36 insertions(+), 0 deletions(-)

test/stage1/behavior/async_fn.zig+36
......@@ -1182,6 +1182,42 @@ test "suspend in for loop" {
11821182 S.doTheTest();
11831183}
11841184
1185test "suspend in while loop" {
1186 const S = struct {
1187 var global_frame: ?anyframe = null;
1188
1189 fn doTheTest() void {
1190 _ = async atest();
1191 while (global_frame) |f| resume f;
1192 }
1193
1194 fn atest() void {
1195 expect(optional(6) == 6);
1196 expect(errunion(6) == 6);
1197 }
1198 fn optional(stuff: ?u32) u32 {
1199 global_frame = @frame();
1200 defer global_frame = null;
1201 while (stuff) |val| {
1202 suspend;
1203 return val;
1204 }
1205 return 0;
1206 }
1207 fn errunion(stuff: anyerror!u32) u32 {
1208 global_frame = @frame();
1209 defer global_frame = null;
1210 while (stuff) |val| {
1211 suspend;
1212 return val;
1213 } else |err| {
1214 return 0;
1215 }
1216 }
1217 };
1218 S.doTheTest();
1219}
1220
11851221test "correctly spill when returning the error union result of another async fn" {
11861222 const S = struct {
11871223 var global_frame: anyframe = undefined;