Skip to content

Commit

Permalink
Suggest replacing 'async' with 'await' at call site
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
etcwilde committed Jan 20, 2021
1 parent 4f708fa commit 0445f4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/Sema/PreCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,15 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
diag.highlight(UDRE->getSourceRange());
typo->addFixits(diag);
} else {
emitBasicError();
if (Name.isSimpleName("async")) {
auto diag = Context.Diags.diagnose(Loc,
diag::cannot_find_in_scope_corrected, Name,
/*isOperator=*/false, "await");
diag.highlight(UDRE->getSourceRange());
diag.fixItReplace(Loc, "await");
} else {
emitBasicError();
}
}

corrections.noteAllCandidates();
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
}

@main struct MyProgram {
static func main() async throws {
// expected-error@+7 {{cannot find 'async' in scope; did you mean 'await'?}}{{9-14=await}}
// expected-error@+6 {{consecutive statements on a line must be separated by ';'}}
// expected-error@+5 {{call is 'async' but is not marked with 'await'}}
// expected-error@+4 {{call can throw but is not marked with 'try'}}
// expected-note@+3 {{did you mean to use 'try'?}}
// expected-note@+2 {{did you mean to handle error as optional value?}}
// expected-note@+1 {{did you mean to disable error propagation?}}
try async asyncFunc()

// expected-error@+4 {{consecutive statements on a line must be separated by ';'}}
// expected-error@+3 {{cannot find 'async' in scope; did you mean 'await'?}}{{13-18=await}}
// expected-warning@+2 {{result of call to 'anotherAsyncFunc()' is unused}}
// expected-error@+1 {{call is 'async' but is not marked with 'await'}}
let _ = async anotherAsyncFunc()

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

0 comments on commit 0445f4f

Please sign in to comment.