authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-24 15:33:20+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-24 15:37:55+02:00
logeabf378a56e0cd9720f43c36b85025228c4406d8
treeaf80f2749253cc4f5c61c14c9f37519dc9146ab8
parent0aede1a8fcd59b259215c05b43827405ad5da9c7

zig fmt: Automagically fix block-less suspend exprs


3 files changed, 43 insertions(+), 3 deletions(-)

lib/std/zig/parse.zig+8-2
...@@ -891,11 +891,17 @@ const Parser = struct {...@@ -891,11 +891,17 @@ const Parser = struct {
891 });891 });
892 },892 },
893 .keyword_suspend => {893 .keyword_suspend => {
894 const token = p.nextToken();
895 // TODO remove this special case when 0.9.0 is released.
896 const block_expr: Node.Index = if (p.eatToken(.semicolon) != null)
897 0
898 else
899 try p.expectBlockExprStatement();
894 return p.addNode(.{900 return p.addNode(.{
895 .tag = .@"suspend",901 .tag = .@"suspend",
896 .main_token = p.nextToken(),902 .main_token = token,
897 .data = .{903 .data = .{
898 .lhs = try p.expectBlockExprStatement(),904 .lhs = block_expr,
899 .rhs = undefined,905 .rhs = undefined,
900 },906 },
901 });907 });
lib/std/zig/parser_test.zig+18
...@@ -40,6 +40,21 @@ test "zig fmt: rewrite inline functions as callconv(.Inline)" {...@@ -40,6 +40,21 @@ test "zig fmt: rewrite inline functions as callconv(.Inline)" {
40 );40 );
41}41}
4242
43// TODO Remove this after zig 0.9.0 is released.
44test "zig fmt: rewrite suspend without block expression" {
45 try testTransform(
46 \\fn foo() void {
47 \\ suspend;
48 \\}
49 \\
50 ,
51 \\fn foo() void {
52 \\ suspend {}
53 \\}
54 \\
55 );
56}
57
43test "zig fmt: simple top level comptime block" {58test "zig fmt: simple top level comptime block" {
44 try testCanonical(59 try testCanonical(
45 \\// line comment60 \\// line comment
...@@ -5023,6 +5038,9 @@ test "recovery: invalid comptime" {...@@ -5023,6 +5038,9 @@ test "recovery: invalid comptime" {
5023}5038}
50245039
5025test "recovery: missing block after suspend" {5040test "recovery: missing block after suspend" {
5041 // TODO Enable this after zig 0.9.0 is released.
5042 if (true) return error.SkipZigTest;
5043
5026 try testError(5044 try testError(
5027 \\fn foo() void {5045 \\fn foo() void {
5028 \\ suspend;5046 \\ suspend;
lib/std/zig/render.zig+17-1
...@@ -255,13 +255,29 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I...@@ -255,13 +255,29 @@ fn renderExpression(gpa: *Allocator, ais: *Ais, tree: ast.Tree, node: ast.Node.I
255 try renderToken(ais, tree, defer_token, .space);255 try renderToken(ais, tree, defer_token, .space);
256 return renderExpression(gpa, ais, tree, expr, space);256 return renderExpression(gpa, ais, tree, expr, space);
257 },257 },
258 .@"comptime", .@"suspend", .@"nosuspend" => {258 .@"comptime", .@"nosuspend" => {
259 const comptime_token = main_tokens[node];259 const comptime_token = main_tokens[node];
260 const block = datas[node].lhs;260 const block = datas[node].lhs;
261 try renderToken(ais, tree, comptime_token, .space);261 try renderToken(ais, tree, comptime_token, .space);
262 return renderExpression(gpa, ais, tree, block, space);262 return renderExpression(gpa, ais, tree, block, space);
263 },263 },
264264
265 .@"suspend" => {
266 const suspend_token = main_tokens[node];
267 const body = datas[node].lhs;
268 if (body != 0) {
269 try renderToken(ais, tree, suspend_token, .space);
270 return renderExpression(gpa, ais, tree, body, space);
271 } else {
272 // TODO remove this special case when 0.9.0 is released.
273 assert(space == .semicolon);
274 try renderToken(ais, tree, suspend_token, .space);
275 try ais.writer().writeAll("{}");
276 try ais.insertNewline();
277 return;
278 }
279 },
280
265 .@"catch" => {281 .@"catch" => {
266 const main_token = main_tokens[node];282 const main_token = main_tokens[node];
267 const fallback_first = tree.firstToken(datas[node].rhs);283 const fallback_first = tree.firstToken(datas[node].rhs);