zig fmt: Automagically fix block-less suspend exprs

This commit is contained in:
LemonBoy
2021-04-24 15:33:20 +02:00
parent 0aede1a8fc
commit eabf378a56
3 changed files with 43 additions and 3 deletions

View File

@@ -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,
},
});

View File

@@ -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;

View File

@@ -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);