Skip to content

Commit 0445f4f

Browse files
committed
Suggest replacing 'async' with 'await' at call site
It's pretty easy to typo-replace 'await' with 'async' and get a confusing error about 'async' not being in scope. This patch updates the diagnostic to suggest replacing 'async' with 'await' at the call-sites so that users aren't left scratching their heads.
1 parent 4f708fa commit 0445f4f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/Sema/PreCheckExpr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,15 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
519519
diag.highlight(UDRE->getSourceRange());
520520
typo->addFixits(diag);
521521
} else {
522-
emitBasicError();
522+
if (Name.isSimpleName("async")) {
523+
auto diag = Context.Diags.diagnose(Loc,
524+
diag::cannot_find_in_scope_corrected, Name,
525+
/*isOperator=*/false, "await");
526+
diag.highlight(UDRE->getSourceRange());
527+
diag.fixItReplace(Loc, "await");
528+
} else {
529+
emitBasicError();
530+
}
523531
}
524532

525533
corrections.noteAllCandidates();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-concurrency -parse-as-library
2+
// REQUIRES: concurrency
3+
4+
func asyncFunc() async throws {}
5+
6+
func anotherAsyncFunc() async -> Int {
7+
return 42
8+
}
9+
10+
@main struct MyProgram {
11+
static func main() async throws {
12+
// expected-error@+7 {{cannot find 'async' in scope; did you mean 'await'?}}{{9-14=await}}
13+
// expected-error@+6 {{consecutive statements on a line must be separated by ';'}}
14+
// expected-error@+5 {{call is 'async' but is not marked with 'await'}}
15+
// expected-error@+4 {{call can throw but is not marked with 'try'}}
16+
// expected-note@+3 {{did you mean to use 'try'?}}
17+
// expected-note@+2 {{did you mean to handle error as optional value?}}
18+
// expected-note@+1 {{did you mean to disable error propagation?}}
19+
try async asyncFunc()
20+
21+
// expected-error@+4 {{consecutive statements on a line must be separated by ';'}}
22+
// expected-error@+3 {{cannot find 'async' in scope; did you mean 'await'?}}{{13-18=await}}
23+
// expected-warning@+2 {{result of call to 'anotherAsyncFunc()' is unused}}
24+
// expected-error@+1 {{call is 'async' but is not marked with 'await'}}
25+
let _ = async anotherAsyncFunc()
26+
27+
// Don't emit a diagnostic here
28+
async let foo = anotherAsyncFunc()
29+
let _ = await foo
30+
}
31+
}

0 commit comments

Comments
 (0)