parser: fix "else" followed by "comptime"

This commit is contained in:
Pavel Verigo 2024-01-26 22:08:30 +01:00 committed by Veikka Tuominen
parent d5fc3c635a
commit 87733171b6
2 changed files with 23 additions and 1 deletions

View File

@ -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,
},
});
}
}

View File

@ -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 {