Fix await in effect with async

Typing `func foo() await {` is something that folks do from time-to-time.
The old error message was unintuitive and suggested adding a semicolon
between the parenthesis and the await, then proceeded to complain about
the opening brace. This wasn't very clear about what the error actually
was.

Pulling this in line with `try`, `throw`, and `throws`, this patch
suggests replacing `await` with `async` when in the function effect
position, and provides a nice fix-it.
This commit is contained in:
Evan Wilde
2022-03-05 01:36:45 -08:00
parent 7abc8a4f44
commit 69cb29e758
3 changed files with 14 additions and 0 deletions

View File

@@ -926,6 +926,7 @@ bool Parser::isEffectsSpecifier(const Token &T) {
// 'parseEffectsSpecifiers()'.
if (T.isContextualKeyword("async") ||
(T.isContextualKeyword("await") && !T.isAtStartOfLine()) ||
T.isContextualKeyword("reasync"))
return true;
@@ -984,6 +985,13 @@ ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
consumeToken();
continue;
}
// diagnose 'await'
if (Tok.isContextualKeyword("await") && !Tok.isAtStartOfLine()) {
diagnose(Tok, diag::await_in_function_type)
.fixItReplace(Tok.getLoc(), "async");
consumeToken();
continue;
}
// 'throws'/'rethrows', or diagnose 'throw'/'try'.
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows) ||