Skip to content

[Concurrency] Suggest replacing 'async' with 'await' at call site #35518

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 3 commits into from
Jan 27, 2021
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
5 changes: 4 additions & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,6 @@ ERROR(snake_case_deprecated,none,
"%0 has been replaced with %1 in Swift 3",
(StringRef, StringRef))


// Assignment statement
ERROR(expected_expr_assignment,none,
"expected expression in assignment", ())
Expand Down Expand Up @@ -986,6 +985,10 @@ NOTE(indent_expression_to_silence,none,
ERROR(expected_expr_throw,PointsToFirstBadToken,
"expected expression in 'throw' statement", ())

// Await/Async
ERROR(expected_await_not_async,none,
"found 'async' in expression; did you mean 'await'?", ())

// Yield Statment
ERROR(expected_expr_yield,PointsToFirstBadToken,
"expected expression in 'yield' statement", ())
Expand Down
23 changes: 16 additions & 7 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,17 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
SyntaxParsingContext ElementContext(SyntaxContext,
SyntaxContextKind::Expr);

if (Tok.isContextualKeyword("await")) {
if (shouldParseExperimentalConcurrency()) {
if (shouldParseExperimentalConcurrency()) {
// A function called "async" is possible, so we don't want to replace it
// with await.
bool isReplaceableAsync = Tok.isContextualKeyword("async") &&
!peekToken().is(tok::l_paren);
if (Tok.isContextualKeyword("await") || isReplaceableAsync) {
// Error on a replaceable async
if (isReplaceableAsync) {
diagnose(Tok.getLoc(), diag::expected_await_not_async)
.fixItReplace(Tok.getLoc(), "await");
}
SourceLoc awaitLoc = consumeToken();
ParserResult<Expr> sub =
parseExprSequenceElement(diag::expected_expr_after_await, isExprBasic);
Expand All @@ -416,12 +425,12 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
sub = makeParserResult(new (Context) AwaitExpr(awaitLoc, sub.get()));
}

return sub;
} else {
// warn that future versions of Swift will parse this token differently.
diagnose(Tok.getLoc(), diag::warn_await_keyword)
.fixItReplace(Tok.getLoc(), "`await`");
return sub;
}
} else if (Tok.isContextualKeyword("await")) {
// warn that future versions of Swift will parse this token differently.
diagnose(Tok.getLoc(), diag::warn_await_keyword)
.fixItReplace(Tok.getLoc(), "`await`");
}

SourceLoc tryLoc;
Expand Down
31 changes: 31 additions & 0 deletions test/Concurrency/await_typo_correction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency -parse-as-library
// REQUIRES: concurrency

func asyncFunc() async throws {}

func anotherAsyncFunc() async -> Int {
return 42
}

func async() throws { }

@main struct MyProgram {
static func main() async throws {
// expected-error@+1 {{found 'async' in expression; did you mean 'await'?}}{{9-14=await}}
try async asyncFunc()

// expected-error@+2 {{found 'async' in expression; did you mean 'await'?}}{{5-10=await}}
// expected-warning@+1 {{'try' must precede 'await'}}{{5-11=}}{{15-15=await }}
async try asyncFunc()

// expected-error@+1 {{found 'async' in expression; did you mean 'await'?}}{{13-18=await}}
let _ = async anotherAsyncFunc()

// Don't emit a diagnostic here
async let foo = anotherAsyncFunc()
let _ = await foo

// I question the name choice, but it's valid
try async()
}
}