authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-08 16:48:12+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-11 17:59:53+02:00
log89e8bb409a6b3dd47f9d1e1ab6e2abc5fb0ef746
tree2e7c12b7874620e757607ca69a7581585adb3dfc
parent2897641fb95a614ae61a444e43a0555804701910

AstGen: use `condbr_inline` if force_comptime is set

The `finishThenElseBlock` would correctly use `break_inline` which would cause Sema to use `addRuntimeBreak` instead of doing the branch at comptime.

2 files changed, 30 insertions(+), 4 deletions(-)

src/AstGen.zig+8-4
...@@ -5294,9 +5294,11 @@ fn orelseCatchExpr(...@@ -5294,9 +5294,11 @@ fn orelseCatchExpr(
5294 // up for this fact by calling rvalue on the else branch.5294 // up for this fact by calling rvalue on the else branch.
5295 const operand = try reachableExpr(&block_scope, &block_scope.base, operand_ri, lhs, rhs);5295 const operand = try reachableExpr(&block_scope, &block_scope.base, operand_ri, lhs, rhs);
5296 const cond = try block_scope.addUnNode(cond_op, operand, node);5296 const cond = try block_scope.addUnNode(cond_op, operand, node);
5297 const condbr = try block_scope.addCondBr(.condbr, node);5297 const condbr_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .condbr_inline else .condbr;
5298 const condbr = try block_scope.addCondBr(condbr_tag, node);
52985299
5299 const block = try parent_gz.makeBlockInst(.block, node);5300 const block_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .block_inline else .block;
5301 const block = try parent_gz.makeBlockInst(block_tag, node);
5300 try block_scope.setBlockBody(block);5302 try block_scope.setBlockBody(block);
5301 // block_scope unstacked now, can add new instructions to parent_gz5303 // block_scope unstacked now, can add new instructions to parent_gz
5302 try parent_gz.instructions.append(astgen.gpa, block);5304 try parent_gz.instructions.append(astgen.gpa, block);
...@@ -5608,9 +5610,11 @@ fn ifExpr(...@@ -5608,9 +5610,11 @@ fn ifExpr(
5608 }5610 }
5609 };5611 };
56105612
5611 const condbr = try block_scope.addCondBr(.condbr, node);5613 const condbr_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .condbr_inline else .condbr;
5614 const condbr = try block_scope.addCondBr(condbr_tag, node);
56125615
5613 const block = try parent_gz.makeBlockInst(.block, node);5616 const block_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .block_inline else .block;
5617 const block = try parent_gz.makeBlockInst(block_tag, node);
5614 try block_scope.setBlockBody(block);5618 try block_scope.setBlockBody(block);
5615 // block_scope unstacked now, can add new instructions to parent_gz5619 // block_scope unstacked now, can add new instructions to parent_gz
5616 try parent_gz.instructions.append(astgen.gpa, block);5620 try parent_gz.instructions.append(astgen.gpa, block);
test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig created+22
...@@ -0,0 +1,22 @@
1pub export fn entry1() void {
2 var x: u32 = 3;
3 _ = @shuffle(u32, [_]u32{0}, @splat(1, @as(u32, 0)), [_]i8{
4 if (x > 1) 1 else -1,
5 });
6}
7
8pub export fn entry2() void {
9 var y: ?i8 = -1;
10 _ = @shuffle(u32, [_]u32{0}, @splat(1, @as(u32, 0)), [_]i8{
11 y orelse 1,
12 });
13}
14
15// error
16// backend=stage2
17// target=native
18//
19// :4:15: error: unable to resolve comptime value
20// :4:15: note: condition in comptime branch must be comptime-known
21// :11:11: error: unable to resolve comptime value
22// :11:11: note: condition in comptime branch must be comptime-known