mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
stage2: Make x and false/x or true comptime-known
Same as preceding change, but for stage2.
This commit is contained in:
parent
2f732deb3d
commit
ca332f57f7
23
src/Sema.zig
23
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(
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user