Commit eabf378a56

LemonBoy <thatlemon@gmail.com>
2021-04-24 15:33:20
zig fmt: Automagically fix block-less suspend exprs
1 parent 0aede1a
Changed files (3)
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,
                     },
                 });
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;
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);