diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-04-02 17:59:07 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-04-02 18:30:44 -0700 |
| commit | 4618c41fa6ca70f06c7e65762d2f38d57b00818c (patch) | |
| tree | 0561268dd3acf7a00e6e7a5ab8f1bb99c08b3999 /test/behavior/eval.zig | |
| parent | 83bb98e13b19d417c9a8de819b98a1566718b993 (diff) | |
| download | zig-4618c41fa6ca70f06c7e65762d2f38d57b00818c.tar.gz zig-4618c41fa6ca70f06c7e65762d2f38d57b00818c.zip | |
Sema: mechanism for converting comptime breaks to runtime
closes #11369
Diffstat (limited to 'test/behavior/eval.zig')
| -rw-r--r-- | test/behavior/eval.zig | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index e3024a3895..c84f3b0e6a 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -893,3 +893,108 @@ test "closure capture type of runtime-known parameter" { var c: i32 = 1234; try S.b(c); } + +test "comptime break passing through runtime condition converted to runtime break" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + const S = struct { + fn doTheTest() !void { + var runtime: u8 = 'b'; + inline for ([3]u8{ 'a', 'b', 'c' }) |byte| { + bar(); + if (byte == runtime) { + foo(byte); + break; + } + } + try expect(ok); + try expect(count == 2); + } + var ok = false; + var count: usize = 0; + + fn foo(byte: u8) void { + ok = byte == 'b'; + } + + fn bar() void { + count += 1; + } + }; + + try S.doTheTest(); +} + +test "comptime break to outer loop passing through runtime condition converted to runtime break" { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + const S = struct { + fn doTheTest() !void { + var runtime: u8 = 'b'; + outer: inline for ([3]u8{ 'A', 'B', 'C' }) |outer_byte| { + inline for ([3]u8{ 'a', 'b', 'c' }) |byte| { + bar(outer_byte); + if (byte == runtime) { + foo(byte); + break :outer; + } + } + } + try expect(ok); + try expect(count == 2); + } + var ok = false; + var count: usize = 0; + + fn foo(byte: u8) void { + ok = byte == 'b'; + } + + fn bar(byte: u8) void { + _ = byte; + count += 1; + } + }; + + try S.doTheTest(); +} + +test "comptime break operand passing through runtime condition converted to runtime break" { + const S = struct { + fn doTheTest(runtime: u8) !void { + const result = inline for ([3]u8{ 'a', 'b', 'c' }) |byte| { + if (byte == runtime) { + break runtime; + } + } else 'z'; + try expect(result == 'b'); + } + }; + + try S.doTheTest('b'); + comptime try S.doTheTest('b'); +} + +test "comptime break operand passing through runtime switch converted to runtime break" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + const S = struct { + fn doTheTest(runtime: u8) !void { + const result = inline for ([3]u8{ 'a', 'b', 'c' }) |byte| { + switch (runtime) { + byte => break runtime, + else => {}, + } + } else 'z'; + try expect(result == 'b'); + } + }; + + try S.doTheTest('b'); + comptime try S.doTheTest('b'); +} |
