diff --git a/lib/std/zig/Parse.zig b/lib/std/zig/Parse.zig index c0b775b8cf..3c007ea1ac 100644 --- 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 index 7f01d72e8c..0513bd9f03 100644 --- 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 {