authorgravatar for paul.verigo@gmail.comPavel Verigo <paul.verigo@gmail.com> 2024-01-26 22:08:30+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-01-27 16:20:47+02:00
log87733171b60b0816e93c76419d10674ba10684e2
tree0f1cb734a8a1c367a969fbcac5d0157228633bac
parentd5fc3c635ad94b6934fb9b90549d7f36eb9fa56b

parser: fix "else" followed by "comptime"


2 files changed, 23 insertions(+), 1 deletions(-)

lib/std/zig/Parse.zig+8-1
...@@ -956,7 +956,14 @@ fn expectStatement(p: *Parse, allow_defer_var: bool) Error!Node.Index {...@@ -956,7 +956,14 @@ fn expectStatement(p: *Parse, allow_defer_var: bool) Error!Node.Index {
956 } else {956 } else {
957 const assign = try p.expectAssignExpr();957 const assign = try p.expectAssignExpr();
958 try p.expectSemicolon(.expected_semi_after_stmt, true);958 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 });
960 }967 }
961 }968 }
962969
lib/std/zig/parser_test.zig+15
...@@ -4376,6 +4376,21 @@ test "zig fmt: invalid doc comments on comptime and test blocks" {...@@ -4376,6 +4376,21 @@ test "zig fmt: invalid doc comments on comptime and test blocks" {
4376 });4376 });
4377}4377}
43784378
4379test "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
4379test "zig fmt: invalid else branch statement" {4394test "zig fmt: invalid else branch statement" {
4380 try testError(4395 try testError(
4381 \\comptime {4396 \\comptime {