Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
parser: fix "else" followed by "comptime"
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelverigo authored and Vexu committed Jan 27, 2024
1 parent d5fc3c6 commit 8773317
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/std/zig/Parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}
}

Expand Down
15 changes: 15 additions & 0 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8773317

Please sign in to comment.