Skip to content

Commit 089151c

Browse files
committed
[Parser] Narrow the Fix-It correcting "async" -> "await" in expressions.
The way in which we detected an "async" in expression context broke some well-formed code that used "async" as a variable. Narrow the check to only cases where the code will be ill-formed with "async" as an identifier.
1 parent 238290c commit 089151c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
398398
SyntaxParsingContext ElementContext(SyntaxContext,
399399
SyntaxContextKind::Expr);
400400

401-
// A function called "async" is possible, so we don't want to replace it
402-
// with await.
401+
// Check whether the user mistyped "async" for "await", but only in cases
402+
// where we are sure that "async" would be ill-formed as an identifier.
403403
bool isReplaceableAsync = Tok.isContextualKeyword("async") &&
404-
!peekToken().is(tok::l_paren);
404+
!peekToken().isAtStartOfLine() &&
405+
(peekToken().is(tok::identifier) || peekToken().is(tok::kw_try));
405406
if (Tok.isContextualKeyword("await") || isReplaceableAsync) {
406407
// Error on a replaceable async
407408
if (isReplaceableAsync) {

test/Concurrency/await_typo_correction.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ func async() throws { }
2929
try async()
3030
}
3131
}
32+
33+
func varNamedAsync(async: Bool) async {
34+
if async { }
35+
let _ = async
36+
}

0 commit comments

Comments
 (0)