authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-09 16:37:35+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-15 00:48:47+03:00
log002260c27481e50c74dbb3d8998cf8c3b1c472d8
tree1dd95be36a6157e8481af89f42972832eb584b4c
parent6f6b14621d5a0935d39295d21b5ffad2197f0e2f

Sema: copy runtime_index & friends when making child blocks


2 files changed, 37 insertions(+), 0 deletions(-)

src/Sema.zig+12
......@@ -4749,6 +4749,9 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
47494749 .inlining = parent_block.inlining,
47504750 .is_comptime = parent_block.is_comptime,
47514751 .c_import_buf = &c_import_buf,
4752 .runtime_cond = parent_block.runtime_cond,
4753 .runtime_loop = parent_block.runtime_loop,
4754 .runtime_index = parent_block.runtime_index,
47524755 };
47534756 defer child_block.instructions.deinit(sema.gpa);
47544757
......@@ -4847,6 +4850,9 @@ fn zirBlock(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErro
48474850 .is_comptime = parent_block.is_comptime,
48484851 .want_safety = parent_block.want_safety,
48494852 .float_mode = parent_block.float_mode,
4853 .runtime_cond = parent_block.runtime_cond,
4854 .runtime_loop = parent_block.runtime_loop,
4855 .runtime_index = parent_block.runtime_index,
48504856 };
48514857
48524858 defer child_block.instructions.deinit(gpa);
......@@ -9742,6 +9748,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
97429748 .inlining = block.inlining,
97439749 .is_comptime = block.is_comptime,
97449750 .switch_else_err_ty = else_error_ty,
9751 .runtime_cond = block.runtime_cond,
9752 .runtime_loop = block.runtime_loop,
9753 .runtime_index = block.runtime_index,
97459754 };
97469755 const merges = &child_block.label.?.merges;
97479756 defer child_block.instructions.deinit(gpa);
......@@ -14872,6 +14881,9 @@ fn zirTypeofPeer(
1487214881 .inlining = block.inlining,
1487314882 .is_comptime = false,
1487414883 .is_typeof = true,
14884 .runtime_cond = block.runtime_cond,
14885 .runtime_loop = block.runtime_loop,
14886 .runtime_index = block.runtime_index,
1487514887 };
1487614888 defer child_block.instructions.deinit(sema.gpa);
1487714889 // Ignore the result, we only care about the instructions in `args`.
test/cases/compile_errors/comptime_store_in_comptime_switch_in_runtime_if.zig created+25
......@@ -0,0 +1,25 @@
1fn foo() bool {
2 return false;
3}
4
5pub export fn entry() void {
6 const Widget = union(enum) { a: u0 };
7
8 comptime var a = 1;
9 const info = @typeInfo(Widget).Union;
10 inline for (info.fields) |field| {
11 if (foo()) {
12 switch (field.field_type) {
13 u0 => a = 2,
14 else => unreachable,
15 }
16 }
17 }
18}
19
20// error
21// backend=stage2
22// target=native
23//
24// :13:27: error: store to comptime variable depends on runtime condition
25// :11:16: note: runtime condition here