diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig index 778075bdee..d6880ad273 100644 --- a/lib/std/zig/parse.zig +++ b/lib/std/zig/parse.zig @@ -891,11 +891,17 @@ const Parser = struct { }); }, .keyword_suspend => { + const token = p.nextToken(); + // TODO remove this special case when 0.9.0 is released. + const block_expr: Node.Index = if (p.eatToken(.semicolon) != null) + 0 + else + try p.expectBlockExprStatement(); return p.addNode(.{ .tag = .@"suspend", - .main_token = p.nextToken(), + .main_token = token, .data = .{ - .lhs = try p.expectBlockExprStatement(), + .lhs = block_expr, .rhs = undefined, }, }); diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 479da2b50c..2fa8baa185 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -40,6 +40,21 @@ test "zig fmt: rewrite inline functions as callconv(.Inline)" { ); } +// TODO Remove this after zig 0.9.0 is released. +test "zig fmt: rewrite suspend without block expression" { + try testTransform( + \\fn foo() void { + \\ suspend; + \\} + \\ + , + \\fn foo() void { + \\ suspend {} + \\} + \\ + ); +} + test "zig fmt: simple top level comptime block" { try testCanonical( \\// line comment @@ -5023,6 +5038,9 @@ test "recovery: invalid comptime" { } test "recovery: missing block after suspend" { + // TODO Enable this after zig 0.9.0 is released. + if (true) return error.SkipZigTest; + try testError( \\fn foo() void { \\ suspend; diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index a417947c6b..a762b78728 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -255,13 +255,29 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I try renderToken(ais, tree, defer_token, .space); return renderExpression(gpa, ais, tree, expr, space); }, - .@"comptime", .@"suspend", .@"nosuspend" => { + .@"comptime", .@"nosuspend" => { const comptime_token = main_tokens[node]; const block = datas[node].lhs; try renderToken(ais, tree, comptime_token, .space); return renderExpression(gpa, ais, tree, block, space); }, + .@"suspend" => { + const suspend_token = main_tokens[node]; + const body = datas[node].lhs; + if (body != 0) { + try renderToken(ais, tree, suspend_token, .space); + return renderExpression(gpa, ais, tree, body, space); + } else { + // TODO remove this special case when 0.9.0 is released. + assert(space == .semicolon); + try renderToken(ais, tree, suspend_token, .space); + try ais.writer().writeAll("{}"); + try ais.insertNewline(); + return; + } + }, + .@"catch" => { const main_token = main_tokens[node]; const fallback_first = tree.firstToken(datas[node].rhs);