authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-15 15:05:31-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-16 01:44:52-05:00
logcb3a818699649a985d5879151936b3b88ffddfa4
treef2f6f383314fa90d329c79c90991dd26428c176b
parent72805fd66e732e29637872f540371c30cb9f8b27
signaturelock-open Commit is signed but in an unrecognized format.

zig fmt: support `noasync await`


5 files changed, 50 insertions(+), 4 deletions(-)

lib/std/zig/ast.zig+3-1
......@@ -1564,7 +1564,9 @@ pub const Node = struct {
15641564 pub const Op = union(enum) {
15651565 AddressOf,
15661566 ArrayType: ArrayInfo,
1567 Await,
1567 Await: struct {
1568 noasync_token: ?TokenIndex = null,
1569 },
15681570 BitNot,
15691571 BoolNot,
15701572 Cancel,
lib/std/zig/parse.zig+14-2
......@@ -1129,7 +1129,7 @@ fn parseErrorUnionExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
11291129/// / KEYWORD_noasync PrimaryTypeExpr SuffixOp* FnCallArguments
11301130/// / PrimaryTypeExpr (SuffixOp / FnCallArguments)*
11311131fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
1132 var maybe_async = eatAnnotatedToken(it, .Keyword_async) orelse eatAnnotatedToken(it, .Keyword_noasync);
1132 const maybe_async = eatAnnotatedToken(it, .Keyword_async) orelse eatAnnotatedToken(it, .Keyword_noasync);
11331133 if (maybe_async) |async_token| {
11341134 const token_fn = eatToken(it, .Keyword_fn);
11351135 if (async_token.ptr.id == .Keyword_async and token_fn != null) {
......@@ -2179,7 +2179,19 @@ fn parsePrefixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
21792179 .MinusPercent => ops{ .NegationWrap = {} },
21802180 .Ampersand => ops{ .AddressOf = {} },
21812181 .Keyword_try => ops{ .Try = {} },
2182 .Keyword_await => ops{ .Await = {} },
2182 .Keyword_await => ops{ .Await = .{} },
2183 .Keyword_noasync => if (eatToken(it, .Keyword_await)) |await_tok| {
2184 const node = try arena.create(Node.PrefixOp);
2185 node.* = Node.PrefixOp{
2186 .op_token = await_tok,
2187 .op = .{ .Await = .{ .noasync_token = token.index } },
2188 .rhs = undefined, // set by caller
2189 };
2190 return &node.base;
2191 } else {
2192 putBackToken(it, token.index);
2193 return null;
2194 },
21832195 else => {
21842196 putBackToken(it, token.index);
21852197 return null;
lib/std/zig/parser_test.zig+9
......@@ -1,3 +1,12 @@
1test "zig fmt: noasync await" {
2 try testCanonical(
3 \\fn foo() void {
4 \\ x = noasync await y;
5 \\}
6 \\
7 );
8}
9
110test "zig fmt: trailing comma in container declaration" {
211 try testCanonical(
312 \\const X = struct { foo: i32 };
lib/std/zig/render.zig+7-1
......@@ -584,12 +584,18 @@ fn renderExpression(
584584 },
585585
586586 .Try,
587 .Await,
588587 .Cancel,
589588 .Resume,
590589 => {
591590 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.Space);
592591 },
592
593 .Await => |await_info| {
594 if (await_info.noasync_token) |tok| {
595 try renderToken(tree, stream, tok, indent, start_col, Space.Space);
596 }
597 try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.Space);
598 },
593599 }
594600
595601 return renderExpression(allocator, stream, tree, indent, start_col, prefix_op_node.rhs, space);
test/stage1/behavior/async_fn.zig+17
......@@ -1510,3 +1510,20 @@ test "take address of temporary async frame" {
15101510 };
15111511 S.doTheTest();
15121512}
1513
1514test "noasync await" {
1515 const S = struct {
1516 fn doTheTest() void {
1517 var frame = async foo(false);
1518 expect(noasync await frame == 42);
1519 }
1520
1521 fn foo(want_suspend: bool) i32 {
1522 if (want_suspend) {
1523 suspend;
1524 }
1525 return 42;
1526 }
1527 };
1528 S.doTheTest();
1529}