Commit a04a98ff3e

Andrew Kelley <andrew@ziglang.org>
2021-06-21 20:12:45
AstGen: while loop continue expr captures in scope
Before this, the continue expression of a while loop did not have the capture variable in it, making it incorrectly emit a compile error for not using the capture, even if it was referenced.
1 parent 4f900e6
Changed files (1)
src/AstGen.zig
@@ -5192,26 +5192,6 @@ fn whileExpr(
     try loop_scope.instructions.append(astgen.gpa, cond_block);
     try continue_scope.setBlockBody(cond_block);
 
-    // This code could be improved to avoid emitting the continue expr when there
-    // are no jumps to it. This happens when the last statement of a while body is noreturn
-    // and there are no `continue` statements.
-    // Tracking issue: https://github.com/ziglang/zig/issues/9185
-    if (while_full.ast.cont_expr != 0) {
-        _ = try expr(&loop_scope, &loop_scope.base, .{ .ty = .void_type }, while_full.ast.cont_expr);
-    }
-    const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;
-    _ = try loop_scope.addNode(repeat_tag, node);
-
-    try loop_scope.setBlockBody(loop_block);
-    loop_scope.break_block = loop_block;
-    loop_scope.continue_block = cond_block;
-    if (while_full.label_token) |label_token| {
-        loop_scope.label = @as(?GenZir.Label, GenZir.Label{
-            .token = label_token,
-            .block_inst = loop_block,
-        });
-    }
-
     var then_scope = parent_gz.makeSubBlock(&continue_scope.base);
     defer then_scope.instructions.deinit(astgen.gpa);
 
@@ -5265,6 +5245,26 @@ fn whileExpr(
         }
     };
 
+    // This code could be improved to avoid emitting the continue expr when there
+    // are no jumps to it. This happens when the last statement of a while body is noreturn
+    // and there are no `continue` statements.
+    // Tracking issue: https://github.com/ziglang/zig/issues/9185
+    if (while_full.ast.cont_expr != 0) {
+        _ = try expr(&loop_scope, then_sub_scope, .{ .ty = .void_type }, while_full.ast.cont_expr);
+    }
+    const repeat_tag: Zir.Inst.Tag = if (is_inline) .repeat_inline else .repeat;
+    _ = try loop_scope.addNode(repeat_tag, node);
+
+    try loop_scope.setBlockBody(loop_block);
+    loop_scope.break_block = loop_block;
+    loop_scope.continue_block = cond_block;
+    if (while_full.label_token) |label_token| {
+        loop_scope.label = @as(?GenZir.Label, GenZir.Label{
+            .token = label_token,
+            .block_inst = loop_block,
+        });
+    }
+
     loop_scope.break_count += 1;
     const then_result = try expr(&then_scope, then_sub_scope, loop_scope.break_result_loc, while_full.ast.then_expr);
     try checkUsed(parent_gz, &then_scope.base, then_sub_scope);