aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-11-08 16:48:12 +0200
committerVeikka Tuominen <git@vexu.eu>2022-11-11 17:59:53 +0200
commit89e8bb409a6b3dd47f9d1e1ab6e2abc5fb0ef746 (patch)
tree2e7c12b7874620e757607ca69a7581585adb3dfc
parent2897641fb95a614ae61a444e43a0555804701910 (diff)
downloadzig-89e8bb409a6b3dd47f9d1e1ab6e2abc5fb0ef746.tar.gz
zig-89e8bb409a6b3dd47f9d1e1ab6e2abc5fb0ef746.zip
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.
-rw-r--r--src/AstGen.zig12
-rw-r--r--test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig22
2 files changed, 30 insertions, 4 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 5006730cad..d876e24977 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -5294,9 +5294,11 @@ fn orelseCatchExpr(
// up for this fact by calling rvalue on the else branch.
const operand = try reachableExpr(&block_scope, &block_scope.base, operand_ri, lhs, rhs);
const cond = try block_scope.addUnNode(cond_op, operand, node);
- const condbr = try block_scope.addCondBr(.condbr, node);
+ const condbr_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .condbr_inline else .condbr;
+ const condbr = try block_scope.addCondBr(condbr_tag, node);
- const block = try parent_gz.makeBlockInst(.block, node);
+ const block_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .block_inline else .block;
+ const block = try parent_gz.makeBlockInst(block_tag, node);
try block_scope.setBlockBody(block);
// block_scope unstacked now, can add new instructions to parent_gz
try parent_gz.instructions.append(astgen.gpa, block);
@@ -5608,9 +5610,11 @@ fn ifExpr(
}
};
- const condbr = try block_scope.addCondBr(.condbr, node);
+ const condbr_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .condbr_inline else .condbr;
+ const condbr = try block_scope.addCondBr(condbr_tag, node);
- const block = try parent_gz.makeBlockInst(.block, node);
+ const block_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .block_inline else .block;
+ const block = try parent_gz.makeBlockInst(block_tag, node);
try block_scope.setBlockBody(block);
// block_scope unstacked now, can add new instructions to parent_gz
try parent_gz.instructions.append(astgen.gpa, block);
diff --git a/test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig b/test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig
new file mode 100644
index 0000000000..92e9620c99
--- /dev/null
+++ b/test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig
@@ -0,0 +1,22 @@
+pub export fn entry1() void {
+ var x: u32 = 3;
+ _ = @shuffle(u32, [_]u32{0}, @splat(1, @as(u32, 0)), [_]i8{
+ if (x > 1) 1 else -1,
+ });
+}
+
+pub export fn entry2() void {
+ var y: ?i8 = -1;
+ _ = @shuffle(u32, [_]u32{0}, @splat(1, @as(u32, 0)), [_]i8{
+ y orelse 1,
+ });
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :4:15: error: unable to resolve comptime value
+// :4:15: note: condition in comptime branch must be comptime-known
+// :11:11: error: unable to resolve comptime value
+// :11:11: note: condition in comptime branch must be comptime-known