diff options
| author | Cody Tapscott <topolarity@tapscott.me> | 2022-10-30 12:25:49 -0700 |
|---|---|---|
| committer | Cody Tapscott <topolarity@tapscott.me> | 2022-10-30 12:38:08 -0700 |
| commit | ca332f57f712c3b4570ca42bc503824022115142 (patch) | |
| tree | 4485ea7741ecbfe501eeb2916290d7c911bfffe0 /test/behavior/eval.zig | |
| parent | 2f732deb3d15752d4977f04b38718e6896460e69 (diff) | |
| download | zig-ca332f57f712c3b4570ca42bc503824022115142.tar.gz zig-ca332f57f712c3b4570ca42bc503824022115142.zip | |
stage2: Make `x and false`/`x or true` comptime-known
Same as preceding change, but for stage2.
Diffstat (limited to 'test/behavior/eval.zig')
| -rw-r--r-- | test/behavior/eval.zig | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 7e46633172..617cc6dfd4 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -1438,3 +1438,53 @@ test "continue nested inline for loop in named block expr" { } try expect(a == 2); } + +test "x and false is comptime-known false" { + const T = struct { + var x: u32 = 0; + + fn foo() bool { + x += 1; // Observable side-effect + return true; + } + }; + + if (T.foo() and T.foo() and false and T.foo()) { + @compileError("Condition should be comptime-known false"); + } + try expect(T.x == 2); + + T.x = 0; + if (T.foo() and T.foo() and b: { + _ = T.foo(); + break :b false; + } and T.foo()) { + @compileError("Condition should be comptime-known false"); + } + try expect(T.x == 3); +} + +test "x or true is comptime-known true" { + const T = struct { + var x: u32 = 0; + + fn foo() bool { + x += 1; // Observable side-effect + return false; + } + }; + + if (!(T.foo() or T.foo() or true or T.foo())) { + @compileError("Condition should be comptime-known false"); + } + try expect(T.x == 2); + + T.x = 0; + if (!(T.foo() or T.foo() or b: { + _ = T.foo(); + break :b true; + } or T.foo())) { + @compileError("Condition should be comptime-known false"); + } + try expect(T.x == 3); +} |
