commit 87733171b60b0816e93c76419d10674ba10684e2 (tree)
parent d5fc3c635ad94b6934fb9b90549d7f36eb9fa56b
Author: Pavel Verigo <paul.verigo@gmail.com>
Date: Fri, 26 Jan 2024 22:08:30 +0100
parser: fix "else" followed by "comptime"
Diffstat:
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/lib/std/zig/Parse.zig b/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,
+ },
+ });
}
}
diff --git a/lib/std/zig/parser_test.zig b/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 {