Skip to content

Commit 8da019c

Browse files
committed
[Syntax] Fix grammar and parsing for 'async'.
I had used the wrong kind of token for the contextual 'async' keyword (consistently), as well as flipping the order of async/throws when creating syntax nodes in the parser. Add a `-verify-syntax-tree` test with valid syntax to ensure that we don't make this mistake again. Problem found by Nate Chandler, thanks!
1 parent 1e95349 commit 8da019c

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

lib/Parse/ParseType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,10 @@ 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());
470468
if (throwsLoc.isValid())
471469
Builder.useThrowsOrRethrowsKeyword(SyntaxContext->popToken());
470+
if (asyncLoc.isValid())
471+
Builder.useAsyncKeyword(SyntaxContext->popToken());
472472

473473
auto InputNode(std::move(*SyntaxContext->popIf<ParsedTypeSyntax>()));
474474
if (auto TupleTypeNode = InputNode.getAs<ParsedTupleTypeSyntax>()) {

test/Parse/async-syntax.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency -verify-syntax-tree
2+
3+
func asyncGlobal1() async { }
4+
func asyncGlobal2() async throws { }
5+
6+
typealias AsyncFunc1 = () async -> ()
7+
typealias AsyncFunc2 = () async throws -> ()
8+
9+
func testTypeExprs() {
10+
let _ = [() async -> ()]()
11+
let _ = [() async throws -> ()]()
12+
}

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
Node('FunctionSignature', kind='Syntax',
7777
children=[
7878
Child('Input', kind='ParameterClause'),
79-
Child('AsyncKeyword', kind='ContextualKeywordToken',
79+
Child('AsyncKeyword', kind='IdentifierToken',
80+
classification='Keyword',
8081
text_choices=['async'], is_optional=True),
8182
Child('ThrowsOrRethrowsKeyword', kind='Token',
8283
is_optional=True,

utils/gyb_syntax_support/ExprNodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@
191191
# NOTE: This appears only in SequenceExpr.
192192
Node('ArrowExpr', kind='Expr',
193193
children=[
194-
Child('AsyncKeyword', kind='ContextualKeywordToken',
194+
Child('AsyncKeyword', kind='IdentifierToken',
195+
classification='Keyword',
195196
text_choices=['async'], is_optional=True),
196197
Child('ThrowsToken', kind='ThrowsToken',
197198
is_optional=True),

utils/gyb_syntax_support/TypeNodes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@
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),
170+
Child('AsyncKeyword', kind='IdentifierToken',
171+
classification='Keyword',
172+
text_choices=['async'], is_optional=True),
172173
Child('ThrowsOrRethrowsKeyword', kind='Token',
173174
is_optional=True,
174175
token_choices=[

0 commit comments

Comments
 (0)