Skip to content

Commit 3cfe390

Browse files
committed
[Concurrency] Add Syntax support for 'async'.
Add 'async' to the syntax tree for types, function declarations, and the "arrow" expression.
1 parent 6029f61 commit 3cfe390

File tree

8 files changed

+50
-7
lines changed

8 files changed

+50
-7
lines changed

lib/Parse/ParseType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
465465
ParsedFunctionTypeSyntaxBuilder Builder(*SyntaxContext);
466466
Builder.useReturnType(std::move(*SyntaxContext->popIf<ParsedTypeSyntax>()));
467467
Builder.useArrow(SyntaxContext->popToken());
468+
if (asyncLoc.isValid())
469+
Builder.useAsyncKeyword(SyntaxContext->popToken());
468470
if (throwsLoc.isValid())
469471
Builder.useThrowsOrRethrowsKeyword(SyntaxContext->popToken());
470472

test/Syntax/Parser/tree.swift.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %swift-syntax-parser-test %s -dump-tree > %t.result
33
// RUN: diff -u %s.result %t.result
44

5-
|func| </t6><t105>|test|</t105><NULL/><s110><s108><t88>|(|</t88><s174></s174><t89>|)| </t89></s108><NULL/><NULL/></s110><NULL/><s93><t90>|{|</t90><s163><s92><s48><NULL/><t102>
5+
|func| </t6><t105>|test|</t105><NULL/><s110><s108><t88>|(|</t88><s174></s174><t89>|)| </t89></s108><NULL/><NULL/><NULL/></s110><NULL/><s93><t90>|{|</t90><s163><s92><s48><NULL/><t102>
66
|"|</t102><s168><s104><t104>|a|</t104></s104><s105><t100>|\|</t100><NULL/><t88>|(|</t88><s165><s97><NULL/><NULL/><s28><t105>|b|</t105><NULL/></s28><NULL/></s97></s165><t101>|)|</t101></s105><s104><t104>|c|</t104></s104></s168><t102>|"|</t102><NULL/></s48><NULL/><NULL/></s92></s163><t91>
77
|}|</t91></s93></s13><NULL/><NULL/></s92></s163><t0>
88
||</t0></s118>

test/incrParse/Outputs/incrementalTransfer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"presence": "Present"
9292
},
9393
null,
94+
null,
9495
null
9596
],
9697
"presence": "Present"

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ FunctionSignatureSyntax getCannedFunctionSignature() {
389389
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, Trivia::spaces(1));
390390
auto Return = SyntaxFactory::makeReturnClause(Arrow, Int);
391391

392-
return SyntaxFactory::makeFunctionSignature(Parameter, Throws, Return);
392+
return SyntaxFactory::makeFunctionSignature(Parameter, None, Throws, Return);
393393
}
394394

395395
TEST(DeclSyntaxTests, FunctionSignatureMakeAPIs) {
@@ -426,7 +426,7 @@ TEST(DeclSyntaxTests, FunctionSignatureGetAPIs) {
426426

427427
auto Sig = SyntaxFactory::makeFunctionSignature(
428428
SyntaxFactory::makeParameterClause(LParen, List, RParen),
429-
Throws,
429+
None, Throws,
430430
SyntaxFactory::makeReturnClause(Arrow, Int));
431431

432432
ASSERT_EQ(LParen.getRaw(), Sig.getInput().getLeftParen().getRaw());

unittests/Syntax/TypeSyntaxTests.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ TEST(TypeSyntaxTests, FunctionTypeMakeAPIs) {
471471
auto Int = SyntaxFactory::makeTypeIdentifier("Int", {}, {});
472472
auto IntArg = SyntaxFactory::makeBlankTupleTypeElement()
473473
.withType(Int);
474+
auto Async = SyntaxFactory::makeContextualKeyword(
475+
"async", { }, { Trivia::spaces(1) });
474476
auto Throws = SyntaxFactory::makeThrowsKeyword({}, { Trivia::spaces(1) });
475477
auto Rethrows = SyntaxFactory::makeRethrowsKeyword({},
476478
{ Trivia::spaces(1) });
@@ -498,13 +500,43 @@ TEST(TypeSyntaxTests, FunctionTypeMakeAPIs) {
498500
SyntaxFactory::makeFunctionType(LeftParen,
499501
TypeList,
500502
RightParen,
503+
Async,
501504
Throws,
502505
Arrow,
503506
Int)
504507
.print(OS);
505-
ASSERT_EQ(OS.str().str(), "(x: Int, y: Int) throws -> Int");
508+
ASSERT_EQ(OS.str().str(), "(x: Int, y: Int) async throws -> Int");
506509
}
507510

511+
{
512+
SmallString<48> Scratch;
513+
llvm::raw_svector_ostream OS(Scratch);
514+
515+
auto x = SyntaxFactory::makeIdentifier("x", {}, {});
516+
auto y = SyntaxFactory::makeIdentifier("y", {}, {});
517+
auto xArg = SyntaxFactory::makeBlankTupleTypeElement()
518+
.withName(x)
519+
.withColon(Colon)
520+
.withType(Int)
521+
.withTrailingComma(Comma);
522+
auto yArg = SyntaxFactory::makeBlankTupleTypeElement()
523+
.withName(y)
524+
.withColon(Colon)
525+
.withType(Int);
526+
527+
auto TypeList = SyntaxFactory::makeTupleTypeElementList({
528+
xArg, yArg
529+
});
530+
SyntaxFactory::makeFunctionType(LeftParen,
531+
TypeList,
532+
RightParen,
533+
None,
534+
Throws,
535+
Arrow,
536+
Int)
537+
.print(OS);
538+
ASSERT_EQ(OS.str().str(), "(x: Int, y: Int) throws -> Int");
539+
}
508540
{
509541
SmallString<48> Scratch;
510542
llvm::raw_svector_ostream OS(Scratch);
@@ -515,6 +547,7 @@ TEST(TypeSyntaxTests, FunctionTypeMakeAPIs) {
515547
SyntaxFactory::makeFunctionType(LeftParen,
516548
TypeList,
517549
RightParen,
550+
None,
518551
Rethrows,
519552
Arrow,
520553
Int).print(OS);
@@ -529,6 +562,7 @@ TEST(TypeSyntaxTests, FunctionTypeMakeAPIs) {
529562
SyntaxFactory::makeFunctionType(LeftParen,
530563
TypeList,
531564
RightParen,
565+
None,
532566
TokenSyntax::missingToken(tok::kw_throws,
533567
"throws"),
534568
Arrow,

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@
7272
]),
7373

7474
# function-signature ->
75-
# '(' parameter-list? ')' (throws | rethrows)? '->'? type?
75+
# '(' parameter-list? ')' async? (throws | rethrows)? '->'? type?
7676
Node('FunctionSignature', kind='Syntax',
7777
children=[
7878
Child('Input', kind='ParameterClause'),
79+
Child('AsyncKeyword', kind='Token', text_choices=['async'],
80+
is_optional=True),
7981
Child('ThrowsOrRethrowsKeyword', kind='Token',
8082
is_optional=True,
8183
token_choices=[

utils/gyb_syntax_support/ExprNodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@
179179
Child('OperatorToken', kind='BinaryOperatorToken'),
180180
]),
181181

182-
# arrow-expr -> 'throws'? '->'
182+
# arrow-expr -> 'async'? 'throws'? '->'
183183
# NOTE: This appears only in SequenceExpr.
184184
Node('ArrowExpr', kind='Expr',
185185
children=[
186+
Child('AsyncKeyword', kind='Token', text_choices=['async'],
187+
is_optional=True),
186188
Child('ThrowsToken', kind='ThrowsToken',
187189
is_optional=True),
188190
Child('ArrowToken', kind='ArrowToken'),

utils/gyb_syntax_support/TypeNodes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,16 @@
159159

160160
# throwing-specifier -> 'throws' | 'rethrows'
161161
# function-type -> attribute-list '(' function-type-argument-list ')'
162-
# throwing-specifier? '->'? type?
162+
# async? throwing-specifier? '->'? type?
163163
Node('FunctionType', kind='Type',
164164
traits=['Parenthesized'],
165165
children=[
166166
Child('LeftParen', kind='LeftParenToken'),
167167
Child('Arguments', kind='TupleTypeElementList',
168168
collection_element_name='Argument'),
169169
Child('RightParen', kind='RightParenToken'),
170+
Child('AsyncKeyword', kind='Token', text_choices=['async'],
171+
is_optional=True),
170172
Child('ThrowsOrRethrowsKeyword', kind='Token',
171173
is_optional=True,
172174
token_choices=[

0 commit comments

Comments
 (0)