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 {
891891 });
892892 },
893893 .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();
894900 return p.addNode(.{
895901 .tag = .@"suspend",
896 .main_token = p.nextToken(),
902 .main_token = token,
897903 .data = .{
898 .lhs = try p.expectBlockExprStatement(),
904 .lhs = block_expr,
899905 .rhs = undefined,
900906 },
901907 });
lib/std/zig/parser_test.zig+18
......@@ -40,6 +40,21 @@ test "zig fmt: rewrite inline functions as callconv(.Inline)" {
4040 );
4141}
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
4358test "zig fmt: simple top level comptime block" {
4459 try testCanonical(
4560 \\// line comment
......@@ -5023,6 +5038,9 @@ test "recovery: invalid comptime" {
50235038}
50245039
50255040test "recovery: missing block after suspend" {
5041 // TODO Enable this after zig 0.9.0 is released.
5042 if (true) return error.SkipZigTest;
5043
50265044 try testError(
50275045 \\fn foo() void {
50285046 \\ 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
255255 try renderToken(ais, tree, defer_token, .space);
256256 return renderExpression(gpa, ais, tree, expr, space);
257257 },
258 .@"comptime", .@"suspend", .@"nosuspend" => {
258 .@"comptime", .@"nosuspend" => {
259259 const comptime_token = main_tokens[node];
260260 const block = datas[node].lhs;
261261 try renderToken(ais, tree, comptime_token, .space);
262262 return renderExpression(gpa, ais, tree, block, space);
263263 },
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
265281 .@"catch" => {
266282 const main_token = main_tokens[node];
267283 const fallback_first = tree.firstToken(datas[node].rhs);