Commit 87733171b6

Pavel Verigo <paul.verigo@gmail.com>
2024-01-26 22:08:30
parser: fix "else" followed by "comptime"
1 parent d5fc3c6
Changed files (2)
lib/std/zig/Parse.zig
@@ -956,7 +956,14 @@ fn expectStatement(p: *Parse, allow_defer_var: bool) Error!Node.Index {
         } else {
             const assign = try p.expectAssignExpr();
             try p.expectSemicolon(.expected_semi_after_stmt, true);
-            return assign;
+            return p.addNode(.{
+                .tag = .@"comptime",
+                .main_token = comptime_token,
+                .data = .{
+                    .lhs = assign,
+                    .rhs = undefined,
+                },
+            });
         }
     }
 
lib/std/zig/parser_test.zig
@@ -4376,6 +4376,21 @@ test "zig fmt: invalid doc comments on comptime and test blocks" {
     });
 }
 
+test "zig fmt: else comptime expr" {
+    try testCanonical(
+        \\comptime {
+        \\    if (true) {} else comptime foo();
+        \\}
+        \\comptime {
+        \\    while (true) {} else comptime foo();
+        \\}
+        \\comptime {
+        \\    for ("") |_| {} else comptime foo();
+        \\}
+        \\
+    );
+}
+
 test "zig fmt: invalid else branch statement" {
     try testError(
         \\comptime {