authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 17:45:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 17:45:38-07:00
log1d5f865cfafdaa96cac35ada9bb9e8b1a4e2bc36
tree2715c03bfc0fff3e2a5845db86019912062d1dfa
parent046df9b7d001120a0142ea29c86cfb3b9ae4eadf

Sema: fix runtime instructions omitted

in the presence of comptime control flow. fixes #12171

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

src/Sema.zig+4
...@@ -4687,6 +4687,10 @@ fn resolveBlockBody(...@@ -4687,6 +4687,10 @@ fn resolveBlockBody(
4687 return sema.analyzeBlockBody(parent_block, src, child_block, merges);4687 return sema.analyzeBlockBody(parent_block, src, child_block, merges);
4688 } else |err| switch (err) {4688 } else |err| switch (err) {
4689 error.ComptimeBreak => {4689 error.ComptimeBreak => {
4690 // Comptime control flow is happening, however child_block may still contain
4691 // runtime instructions which need to be copied to the parent block.
4692 try parent_block.instructions.appendSlice(sema.gpa, child_block.instructions.items);
4693
4690 const break_inst = sema.comptime_break_inst;4694 const break_inst = sema.comptime_break_inst;
4691 const break_data = sema.code.instructions.items(.data)[break_inst].@"break";4695 const break_data = sema.code.instructions.items(.data)[break_inst].@"break";
4692 if (break_data.block_inst == body_inst) {4696 if (break_data.block_inst == body_inst) {
test/behavior/eval.zig+11
...@@ -1261,3 +1261,14 @@ test "comptime write through extern struct reinterpreted as array" {...@@ -1261,3 +1261,14 @@ test "comptime write through extern struct reinterpreted as array" {
1261 assert(s.c == 3);1261 assert(s.c == 3);
1262 }1262 }
1263}1263}
1264
1265test "continue nested in a conditional in an inline for" {
1266 var x: u32 = 1;
1267 inline for ([_]u8{ 1, 2, 3 }) |_| {
1268 if (1 == 1) {
1269 x = 0;
1270 continue;
1271 }
1272 }
1273 try expect(x == 0);
1274}