diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-10-30 22:11:44 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-30 22:11:44 -0400 |
| commit | ef761c2cbc5c9a06da5c09de389a1d778731d170 (patch) | |
| tree | 5a3916c4bad25344395cde1913548f800f79aa05 /src/Sema.zig | |
| parent | 6d999abba0fc4470f9bb2dae8997fded9bff60c4 (diff) | |
| parent | ca332f57f712c3b4570ca42bc503824022115142 (diff) | |
| download | zig-ef761c2cbc5c9a06da5c09de389a1d778731d170.tar.gz zig-ef761c2cbc5c9a06da5c09de389a1d778731d170.zip | |
Merge pull request #13360 from topolarity/comptime-bool-binops
Make `x and false` and `x or true` comptime-known
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 39f57ad1f2..95fae28e82 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -15931,12 +15931,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 @@ -15976,7 +15974,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( |
