Skip to content

Commit 8773317

Browse files
pavelverigoVexu
authored andcommitted
parser: fix "else" followed by "comptime"
1 parent d5fc3c6 commit 8773317

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/std/zig/Parse.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,14 @@ fn expectStatement(p: *Parse, allow_defer_var: bool) Error!Node.Index {
956956
} else {
957957
const assign = try p.expectAssignExpr();
958958
try p.expectSemicolon(.expected_semi_after_stmt, true);
959-
return assign;
959+
return p.addNode(.{
960+
.tag = .@"comptime",
961+
.main_token = comptime_token,
962+
.data = .{
963+
.lhs = assign,
964+
.rhs = undefined,
965+
},
966+
});
960967
}
961968
}
962969

lib/std/zig/parser_test.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4376,6 +4376,21 @@ test "zig fmt: invalid doc comments on comptime and test blocks" {
43764376
});
43774377
}
43784378

4379+
test "zig fmt: else comptime expr" {
4380+
try testCanonical(
4381+
\\comptime {
4382+
\\ if (true) {} else comptime foo();
4383+
\\}
4384+
\\comptime {
4385+
\\ while (true) {} else comptime foo();
4386+
\\}
4387+
\\comptime {
4388+
\\ for ("") |_| {} else comptime foo();
4389+
\\}
4390+
\\
4391+
);
4392+
}
4393+
43794394
test "zig fmt: invalid else branch statement" {
43804395
try testError(
43814396
\\comptime {

0 commit comments

Comments
 (0)