Skip to content

[Concurrency] Treat 'await' as a contextual keyword. #33457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
/// parseExprSequenceElement
///
/// expr-sequence-element(Mode):
/// '__await' expr-sequence-element(Mode)
/// 'await' expr-sequence-element(Mode)
/// 'try' expr-sequence-element(Mode)
/// 'try' '?' expr-sequence-element(Mode)
/// 'try' '!' expr-sequence-element(Mode)
Expand All @@ -392,8 +392,8 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
SyntaxParsingContext ElementContext(SyntaxContext,
SyntaxContextKind::Expr);

if (shouldParseExperimentalConcurrency() && Tok.is(tok::kw___await)) {
SourceLoc awaitLoc = consumeToken(tok::kw___await);
if (shouldParseExperimentalConcurrency() && Tok.isContextualKeyword("await")) {
SourceLoc awaitLoc = consumeToken();
ParserResult<Expr> sub = parseExprUnary(message, isExprBasic);
if (!sub.hasCodeCompletion() && !sub.isNull()) {
ElementContext.setCreateSyntax(SyntaxKind::AwaitExpr);
Expand Down
6 changes: 5 additions & 1 deletion test/Parse/async-syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ func testTypeExprs() {
}

func testAwaitOperator() async {
let _ = __await asyncGlobal1()
let _ = await asyncGlobal1()
}

func testAsyncClosure() {
let _ = { () async in 5 }
let _ = { () throws in 5 }
let _ = { () async throws in 5 }
}

func testAwait() async {
let _ = await asyncGlobal1()
}
11 changes: 11 additions & 0 deletions test/Parse/async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ func testTypeExprs() {

let _ = [() -> async ()]() // expected-error{{'async' may only occur before '->'}}{{18-24=}}{{15-15=async }}
}

// Parsing await syntax.
struct MyFuture {
func await() -> Int { 0 }
}

func testAwaitExpr() async {
let _ = await asyncGlobal1()
let myFuture = MyFuture()
let _ = myFuture.await()
}
2 changes: 1 addition & 1 deletion test/Syntax/Parser/async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func testTypeExprs() {
}

func testAwaitOperator() async {
let _ = __await asyncGlobal1()
let _ = await asyncGlobal1()
}

func testAsyncClosure() {
Expand Down
36 changes: 18 additions & 18 deletions test/expr/unary/async_await.swift
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
// RUN: %target-swift-frontend -typecheck -verify %s -enable-experimental-concurrency

func test1(asyncfp : () async -> Int, fp : () -> Int) async {
_ = __await asyncfp()
_ = __await asyncfp() + asyncfp()
_ = __await asyncfp() + fp()
_ = __await fp() + 42 // expected-warning {{no calls to 'async' functions occur within 'await' expression}}
_ = await asyncfp()
_ = await asyncfp() + asyncfp()
_ = await asyncfp() + fp()
_ = await fp() + 42 // expected-warning {{no calls to 'async' functions occur within 'await' expression}}
_ = asyncfp() // expected-error {{call is 'async' but is not marked with 'await'}}
}

func getInt() async -> Int { return 5 }

// Locations where "await" is prohibited.
func test2(
defaulted: Int = __await getInt() // expected-error{{'async' call cannot occur in a default argument}}
defaulted: Int = await getInt() // expected-error{{'async' call cannot occur in a default argument}}
) async {
defer {
_ = __await getInt() // expected-error{{'async' call cannot occur in a defer body}}
_ = await getInt() // expected-error{{'async' call cannot occur in a defer body}}
}
print("foo")
}

func test3() { // expected-note{{add 'async' to function 'test3()' to make it asynchronous}}
_ = __await getInt() // expected-error{{'async' in a function that does not support concurrency}}
_ = await getInt() // expected-error{{'async' in a function that does not support concurrency}}
}

enum SomeEnum: Int {
case foo = __await 5 // expected-error{{raw value for enum case must be a literal}}
case foo = await 5 // expected-error{{raw value for enum case must be a literal}}
}

struct SomeStruct {
var x = __await getInt() // expected-error{{'async' call cannot occur in a property initializer}}
static var y = __await getInt() // expected-error{{'async' call cannot occur in a global variable initializer}}
var x = await getInt() // expected-error{{'async' call cannot occur in a property initializer}}
static var y = await getInt() // expected-error{{'async' call cannot occur in a global variable initializer}}
}

func acceptAutoclosureNonAsync(_: @autoclosure () -> Int) async { }
Expand All @@ -46,27 +46,27 @@ struct HasAsyncBad {
}

func testAutoclosure() async {
__await acceptAutoclosureAsync(getInt()) // expected-error{{call is 'async' in an autoclosure argument is not marked with 'await'}}
__await acceptAutoclosureNonAsync(getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}
await acceptAutoclosureAsync(getInt()) // expected-error{{call is 'async' in an autoclosure argument is not marked with 'await'}}
await acceptAutoclosureNonAsync(getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}

__await acceptAutoclosureAsync(__await getInt())
__await acceptAutoclosureNonAsync(__await getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}
await acceptAutoclosureAsync(await getInt())
await acceptAutoclosureNonAsync(await getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}

__await acceptAutoclosureAsync(getInt()) // expected-error{{call is 'async' in an autoclosure argument is not marked with 'await'}}
__await acceptAutoclosureNonAsync(getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}
await acceptAutoclosureAsync(getInt()) // expected-error{{call is 'async' in an autoclosure argument is not marked with 'await'}}
await acceptAutoclosureNonAsync(getInt()) // expected-error{{'async' in an autoclosure that does not support concurrency}}
}

// Test inference of 'async' from the body of a closure.
func testClosure() {
let closure = {
__await getInt()
await getInt()
}

let _: () -> Int = closure // expected-error{{cannot convert value of type '() async -> Int' to specified type '() -> Int'}}

let closure2 = { () async -> Int in
print("here")
return __await getInt()
return await getInt()
}

let _: () -> Int = closure2 // expected-error{{cannot convert value of type '() async -> Int' to specified type '() -> Int'}}
Expand Down
4 changes: 3 additions & 1 deletion utils/gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
# await foo()
Node('AwaitExpr', kind='Expr',
children=[
Child('AwaitKeyword', kind='AwaitToken'),
Child('AwaitKeyword', kind='IdentifierToken',
classification='Keyword',
text_choices=['await']),
Child('Expression', kind='Expr'),
]),

Expand Down
1 change: 0 additions & 1 deletion utils/gyb_syntax_support/Token.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ def macro_name(self):
ExprKeyword('True', 'true', serialization_code=51),
ExprKeyword('Try', 'try', serialization_code=52),
ExprKeyword('Throws', 'throws', serialization_code=53),
ExprKeyword('Await', '__await', serialization_code=123),

Keyword('__FILE__', '__FILE__', serialization_code=54),
Keyword('__LINE__', '__LINE__', serialization_code=55),
Expand Down