Skip to content

Commit 69cb29e

Browse files
committed
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.
1 parent 7abc8a4 commit 69cb29e

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ ERROR(expected_dynamic_func_attr,none,
804804
ERROR(async_after_throws,none,
805805
"%select{'async'|'reasync'}0 must precede %select{'throws'|'rethrows'}1",
806806
(bool, bool))
807+
ERROR(await_in_function_type,none,
808+
"expected async specifier; did you mean 'async'?", ())
807809
ERROR(duplicate_effects_specifier,none,
808810
"'%0' has already been specified", (StringRef))
809811

lib/Parse/ParsePattern.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ bool Parser::isEffectsSpecifier(const Token &T) {
926926
// 'parseEffectsSpecifiers()'.
927927

928928
if (T.isContextualKeyword("async") ||
929+
(T.isContextualKeyword("await") && !T.isAtStartOfLine()) ||
929930
T.isContextualKeyword("reasync"))
930931
return true;
931932

@@ -984,6 +985,13 @@ ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
984985
consumeToken();
985986
continue;
986987
}
988+
// diagnose 'await'
989+
if (Tok.isContextualKeyword("await") && !Tok.isAtStartOfLine()) {
990+
diagnose(Tok, diag::await_in_function_type)
991+
.fixItReplace(Tok.getLoc(), "async");
992+
consumeToken();
993+
continue;
994+
}
987995

988996
// 'throws'/'rethrows', or diagnose 'throw'/'try'.
989997
if (Tok.isAny(tok::kw_throws, tok::kw_rethrows) ||

test/Parse/errors.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,7 @@ func fixitTry0<T>(a: T) try where T:ExpressibleByStringLiteral {} // expected-er
160160
func fixitTry1<T>(a: T) try {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{25-28=throws}}
161161
func fixitTry2() try {} // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{18-21=throws}}
162162
let fixitTry3 : () try -> Int // expected-error{{expected throwing specifier; did you mean 'throws'?}} {{20-23=throws}}
163+
164+
func fixitAwait0() await { } // expected-error{{expected async specifier; did you mean 'async'?}}{{20-25=async}}
165+
func fixitAwait1() await -> Int { } // expected-error{{expected async specifier; did you mean 'async'?}}{{20-25=async}}
166+
func fixitAwait2() throws await -> Int { } // expected-error{{expected async specifier; did you mean 'async'?}}{{27-32=async}}

0 commit comments

Comments
 (0)