From ca332f57f712c3b4570ca42bc503824022115142 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Sun, 30 Oct 2022 12:25:49 -0700 Subject: [PATCH] stage2: Make `x and false`/`x or true` comptime-known Same as preceding change, but for stage2. --- src/Sema.zig | 23 +++++++++++++------ test/behavior/eval.zig | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index a73c1eedcb..34b22f5073 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -15932,12 +15932,10 @@ fn zirBoolBr( const gpa = sema.gpa; if (try sema.resolveDefinedValue(parent_block, lhs_src, lhs)) |lhs_val| { - if (lhs_val.toBool() == is_bool_or) { - if (is_bool_or) { - return Air.Inst.Ref.bool_true; - } else { - return Air.Inst.Ref.bool_false; - } + if (is_bool_or and lhs_val.toBool()) { + return Air.Inst.Ref.bool_true; + } else if (!is_bool_or and !lhs_val.toBool()) { + return Air.Inst.Ref.bool_false; } // comptime-known left-hand side. No need for a block here; the result // is simply the rhs expression. Here we rely on there only being 1 @@ -15977,7 +15975,18 @@ fn zirBoolBr( _ = try rhs_block.addBr(block_inst, rhs_result); } - return finishCondBr(sema, parent_block, &child_block, &then_block, &else_block, lhs, block_inst); + const result = finishCondBr(sema, parent_block, &child_block, &then_block, &else_block, lhs, block_inst); + if (!sema.typeOf(rhs_result).isNoReturn()) { + if (try sema.resolveDefinedValue(rhs_block, sema.src, rhs_result)) |rhs_val| { + if (is_bool_or and rhs_val.toBool()) { + return Air.Inst.Ref.bool_true; + } else if (!is_bool_or and !rhs_val.toBool()) { + return Air.Inst.Ref.bool_false; + } + } + } + + return result; } fn finishCondBr( 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); +}