Commit 1d5f865cfa

Andrew Kelley <andrew@ziglang.org>
2022-07-20 02:45:38
Sema: fix runtime instructions omitted
in the presence of comptime control flow. fixes #12171
1 parent 046df9b
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -4687,6 +4687,10 @@ fn resolveBlockBody(
             return sema.analyzeBlockBody(parent_block, src, child_block, merges);
         } else |err| switch (err) {
             error.ComptimeBreak => {
+                // Comptime control flow is happening, however child_block may still contain
+                // runtime instructions which need to be copied to the parent block.
+                try parent_block.instructions.appendSlice(sema.gpa, child_block.instructions.items);
+
                 const break_inst = sema.comptime_break_inst;
                 const break_data = sema.code.instructions.items(.data)[break_inst].@"break";
                 if (break_data.block_inst == body_inst) {
test/behavior/eval.zig
@@ -1261,3 +1261,14 @@ test "comptime write through extern struct reinterpreted as array" {
         assert(s.c == 3);
     }
 }
+
+test "continue nested in a conditional in an inline for" {
+    var x: u32 = 1;
+    inline for ([_]u8{ 1, 2, 3 }) |_| {
+        if (1 == 1) {
+            x = 0;
+            continue;
+        }
+    }
+    try expect(x == 0);
+}