Skip to content

Commit 31e25c3

Browse files
James BrownJames Brown
authored andcommitted
issue #65913 - Correct wrong insertion point suggestion
when using shorthand optional binding by looking for implicitDeclRefExpr that points to a VarDecl. If we have that, get the identifier and insert ' = await (identifier)'.
1 parent 3990e17 commit 31e25c3

File tree

2 files changed

+70
-10
lines changed

2 files changed

+70
-10
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,7 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
28012801

28022802
static bool isEffectAnchor(Expr *e) {
28032803
return isa<AbstractClosureExpr>(e) || isa<DiscardAssignmentExpr>(e) ||
2804-
isa<AssignExpr>(e);
2804+
isa<AssignExpr>(e) || (isa<DeclRefExpr>(e) && e->isImplicit());
28052805
}
28062806

28072807
static bool isAnchorTooEarly(Expr *e) {
@@ -3581,27 +3581,43 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
35813581
Ctx.Diags.diagnose(E->getAwaitLoc(), diag::no_async_in_await);
35823582
}
35833583

3584-
void diagnoseUncoveredAsyncSite(const Expr *anchor) const {
3585-
auto asyncPointIter = uncoveredAsync.find(anchor);
3586-
if (asyncPointIter == uncoveredAsync.end())
3587-
return;
3588-
const std::vector<DiagnosticInfo> &errors = asyncPointIter->getSecond();
3584+
std::pair<SourceLoc, std::string>
3585+
getFixItForUncoveredAsyncSite(const Expr *anchor) const {
35893586
SourceLoc awaitInsertLoc = anchor->getStartLoc();
3590-
if (const AnyTryExpr *tryExpr = dyn_cast<AnyTryExpr>(anchor))
3587+
std::string insertText = "await ";
3588+
if (auto *tryExpr = dyn_cast<AnyTryExpr>(anchor))
35913589
awaitInsertLoc = tryExpr->getSubExpr()->getStartLoc();
3592-
else if (const AutoClosureExpr *autoClosure = dyn_cast<AutoClosureExpr>(anchor)) {
3593-
if (const AnyTryExpr *tryExpr = dyn_cast<AnyTryExpr>(autoClosure->getSingleExpressionBody()))
3590+
else if (auto *autoClosure = dyn_cast<AutoClosureExpr>(anchor)) {
3591+
if (auto *tryExpr =
3592+
dyn_cast<AnyTryExpr>(autoClosure->getSingleExpressionBody()))
35943593
awaitInsertLoc = tryExpr->getSubExpr()->getStartLoc();
3594+
// Supply a tailored fixIt including the identifier if we are
3595+
// looking at a shorthand optional binding.
3596+
} else if (anchor->isImplicit()) {
3597+
if (auto declRef = dyn_cast<DeclRefExpr>(anchor))
3598+
if (auto var = dyn_cast_or_null<VarDecl>(declRef->getDecl())) {
3599+
insertText = " = await " + var->getNameStr().str();
3600+
awaitInsertLoc = Lexer::getLocForEndOfToken(Ctx.Diags.SourceMgr,
3601+
anchor->getStartLoc());
3602+
}
35953603
}
3604+
return std::make_pair(awaitInsertLoc, insertText);
3605+
}
35963606

3607+
void diagnoseUncoveredAsyncSite(const Expr *anchor) const {
3608+
auto asyncPointIter = uncoveredAsync.find(anchor);
3609+
if (asyncPointIter == uncoveredAsync.end())
3610+
return;
3611+
const auto &errors = asyncPointIter->getSecond();
3612+
const auto &[loc, insertText] = getFixItForUncoveredAsyncSite(anchor);
35973613
bool downgradeToWarning = llvm::all_of(errors,
35983614
[&](DiagnosticInfo diag) -> bool {
35993615
return diag.downgradeToWarning;
36003616
});
36013617

36023618
Ctx.Diags.diagnose(anchor->getStartLoc(), diag::async_expr_without_await)
36033619
.warnUntilSwiftVersionIf(downgradeToWarning, 6)
3604-
.fixItInsert(awaitInsertLoc, "await ")
3620+
.fixItInsert(loc, insertText)
36053621
.highlight(anchor->getSourceRange());
36063622

36073623
for (const DiagnosticInfo &diag: errors) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking
2+
3+
// REQUIRES: concurrency
4+
class A {}
5+
class B: A {}
6+
typealias Foo = (name: String, age: Int)
7+
func f(_ x: String) async -> String? { x }
8+
func test() async {
9+
async let result: B? = nil
10+
async let person: Foo? = nil
11+
if let result: A = result {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{22-22=await }}
12+
// expected-warning@-1 {{immutable value 'result' was never used; consider replacing with '_' or removing it}}
13+
// expected-note@-2 {{reference to async let 'result' is 'async'}}
14+
if let result: A {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{19-19= = await result}}
15+
// expected-warning@-1 {{immutable value 'result' was never used; consider replacing with '_' or removing it}}
16+
// expected-note@-2 {{reference to async let 'result' is 'async'}}
17+
if let result = result {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{19-19=await }}
18+
// expected-warning@-1 {{value 'result' was defined but never used; consider replacing with boolean test}}
19+
// expected-note@-2 {{reference to async let 'result' is 'async'}}
20+
if let result {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{16-16= = await result}}
21+
// expected-warning@-1 {{value 'result' was defined but never used; consider replacing with boolean test}}
22+
// expected-note@-2 {{reference to async let 'result' is 'async'}}
23+
if let person: Foo = person {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{24-24=await }}
24+
// expected-warning@-1 {{immutable value 'person' was never used; consider replacing with '_' or removing it}}
25+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
26+
if let person: Foo {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{21-21= = await person}}
27+
// expected-warning@-1 {{immutable value 'person' was never used; consider replacing with '_' or removing it}}
28+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
29+
if let person: (String, Int) = person {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{34-34=await }}
30+
// expected-warning@-1 {{immutable value 'person' was never used; consider replacing with '_' or removing it}}
31+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
32+
if let person: (String, Int) {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{31-31= = await person}}
33+
// expected-warning@-1 {{immutable value 'person' was never used; consider replacing with '_' or removing it}}
34+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
35+
if let person = person {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{19-19=await }}
36+
// expected-warning@-1 {{value 'person' was defined but never used; consider replacing with boolean test}}
37+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
38+
if let person {} // expected-error {{expression is 'async' but is not marked with 'await'}} {{16-16= = await person}}
39+
// expected-warning@-1 {{value 'person' was defined but never used; consider replacing with boolean test}}
40+
// expected-note@-2 {{reference to async let 'person' is 'async'}}
41+
let a = f("a") // expected-error {{expression is 'async' but is not marked with 'await'}} {{11-11=await }}
42+
// expected-warning@-1 {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
43+
// expected-note@-2 {{call is 'async'}}
44+
}

0 commit comments

Comments
 (0)