Skip to content

Commit 7d3e40b

Browse files
authored
Merge pull request #85830 from jamieQ/no-async-in-await-fixit
[Sema]: add fixit for `no_async_in_await` warning
2 parents f89131d + 87aab10 commit 7d3e40b

17 files changed

+134
-107
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4667,13 +4667,15 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
46674667
void diagnoseRedundantAwait(AwaitExpr *E) const {
46684668
if (auto *SVE = SingleValueStmtExpr::tryDigOutSingleValueStmtExpr(E)) {
46694669
// For an if/switch expression, produce a tailored warning.
4670-
Ctx.Diags.diagnose(E->getAwaitLoc(),
4671-
diag::effect_marker_on_single_value_stmt,
4672-
"await", SVE->getStmt()->getKind())
4673-
.highlight(E->getAwaitLoc());
4670+
Ctx.Diags
4671+
.diagnose(E->getAwaitLoc(), diag::effect_marker_on_single_value_stmt,
4672+
"await", SVE->getStmt()->getKind())
4673+
.highlight(E->getAwaitLoc())
4674+
.fixItRemove(E->getAwaitLoc());
46744675
return;
46754676
}
4676-
Ctx.Diags.diagnose(E->getAwaitLoc(), diag::no_async_in_await);
4677+
Ctx.Diags.diagnose(E->getAwaitLoc(), diag::no_async_in_await)
4678+
.fixItRemove(E->getAwaitLoc());
46774679
}
46784680

46794681
void diagnoseRedundantUnsafe(UnsafeExpr *E) const {

test/Concurrency/actor_call_implicitly_async.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ actor BankAccount {
5050
}
5151

5252
func testSelfBalance() async {
53-
_ = await balance() // expected-warning {{no 'async' operations occur within 'await' expression}}
53+
_ = await balance() // expected-warning {{no 'async' operations occur within 'await' expression}}{{9-15=}}
5454
}
5555

5656
// returns the amount actually withdrawn
@@ -357,12 +357,12 @@ actor Calculator {
357357
// We will error on the next line when we get past type checking. But since we
358358
// error in the type checker, we do not make further progress.
359359
let _ = (await bananaAdd(1))(2)
360-
let _ = await (await bananaAdd(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}
360+
let _ = await (await bananaAdd(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}{{11-17=}}
361361

362362
let calc = Calculator()
363363

364364
let _ = (await calc.addCurried(1))(2)
365-
let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}
365+
let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}{{11-17=}}
366366

367367
let plusOne = await calc.addCurried(await calc.add(0, 1))
368368
let _ = plusOne(2)

test/Concurrency/actor_existentials.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ actor Act {
5454
nonisolated let act = Act()
5555

5656
func bad() async {
57-
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}
57+
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}{{5-11=}}
5858
// expected-error@+2 {{actor-isolated property 'i' can not be mutated from a nonisolated context}}
5959
// expected-note@+1 {{consider declaring an isolated method on 'Act' to perform the mutation}}
6060
await act.i = 666
@@ -66,13 +66,13 @@ protocol Proto: Actor {
6666
extension Act: Proto {}
6767

6868
func good() async {
69-
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}
69+
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}{{5-11=}}
7070
// expected-error@+2 {{actor-isolated property 'i' can not be mutated from a nonisolated context}}
7171
// expected-note@+1 {{consider declaring an isolated method on 'Proto' to perform the mutation}}
7272
await (act as any Proto).i = 42
7373
let aIndirect: any Proto = act
7474

75-
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}
75+
// expected-warning@+3 {{no 'async' operations occur within 'await' expression}}{{5-11=}}
7676
// expected-error@+2 {{actor-isolated property 'i' can not be mutated from a nonisolated context}}
7777
// expected-note@+1 {{consider declaring an isolated method on 'Proto' to perform the mutation}}
7878
await aIndirect.i = 777

test/Concurrency/actor_isolation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ extension MyActor {
275275
// expected-note@-1{{consider declaring an isolated method on 'MyActor' to perform the mutation}}
276276
acceptInout(&otherActor.mutable) // expected-error{{actor-isolated property 'mutable' can not be used 'inout' on a nonisolated actor instance}}
277277
// expected-error@+3{{actor-isolated property 'mutable' can not be mutated on a nonisolated actor instance}}
278-
// expected-warning@+2{{no 'async' operations occur within 'await' expression}}
278+
// expected-warning@+2{{no 'async' operations occur within 'await' expression}}{{5-11=}}
279279
// expected-note@+1{{consider declaring an isolated method on 'MyActor' to perform the mutation}}
280280
await otherActor.mutable = 0
281281

@@ -616,7 +616,7 @@ func testGlobalRestrictions(actor: MyActor) async {
616616

617617
// stored and computed properties can be accessed. Only immutable stored properties can be accessed without 'await'
618618
_ = actor.immutable
619-
_ = await actor.immutable // expected-warning {{no 'async' operations occur within 'await' expression}}
619+
_ = await actor.immutable // expected-warning {{no 'async' operations occur within 'await' expression}}{{7-13=}}
620620
_ = actor.mutable // expected-error{{actor-isolated property 'mutable' cannot be accessed from outside of the actor}}{{7-7=await }}
621621
_ = await actor.mutable
622622
_ = actor.text[0] // expected-error{{actor-isolated property 'text' cannot be accessed from outside of the actor}}{{7-7=await }}
@@ -1205,7 +1205,7 @@ extension MyActor {
12051205
}
12061206

12071207
acceptAsyncSendableClosureInheriting {
1208-
_ = await synchronous() // expected-warning{{no 'async' operations occur within 'await' expression}}
1208+
_ = await synchronous() // expected-warning{{no 'async' operations occur within 'await' expression}}{{11-17=}}
12091209
counter += 1 // okay
12101210
}
12111211

test/Concurrency/actor_isolation_swift6.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ func checkIsolationValueType(_ formance: InferredFromConformance,
5353
_ = anno.counter
5454

5555
// make sure it's just a warning if someone was awaiting on it previously
56-
_ = await ext.point // expected-warning {{no 'async' operations occur within 'await' expression}}
57-
_ = await formance.counter // expected-warning {{no 'async' operations occur within 'await' expression}}
58-
_ = await anno.counter // expected-warning {{no 'async' operations occur within 'await' expression}}
56+
_ = await ext.point // expected-warning {{no 'async' operations occur within 'await' expression}}{{7-13=}}
57+
_ = await formance.counter // expected-warning {{no 'async' operations occur within 'await' expression}}{{7-13=}}
58+
_ = await anno.counter // expected-warning {{no 'async' operations occur within 'await' expression}}{{7-13=}}
5959

6060
// this does not need an await, since the property is 'Sendable' and of a
6161
// value type
6262
_ = anno.point
6363
_ = await anno.point
64-
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}
64+
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}{{7-13=}}
6565

6666
// these do need await, regardless of reference or value type
6767
_ = await (formance as any MainCounter).counter

test/Concurrency/effectful_properties_async_if_optional_unwrap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct Kappa {
4848
// though we could try to could give a better message
4949
if let await maybeData { // expected-error{{unwrap condition requires a valid identifier}}
5050
// expected-error@-1{{pattern variable binding cannot appear in an expression}}
51-
// expected-warning@-2{{no 'async' operations occur within 'await' expression}}
51+
// expected-warning@-2{{no 'async' operations occur within 'await' expression}}{{12-18=}}
5252
return maybeData // expected-error{{expression is 'async' but is not marked with 'await'}}
5353
// expected-note@-1{{property access is 'async'}}
5454
}

test/Concurrency/global_actor_inference.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ func acceptAsyncSendableClosureInheriting<T>(@_inheritActorContext _: @Sendable
653653
}
654654

655655
acceptAsyncSendableClosureInheriting {
656-
await onlyOnMainActor() // expected-warning{{no 'async' operations occur within 'await' expression}}
656+
await onlyOnMainActor() // expected-warning{{no 'async' operations occur within 'await' expression}}{{5-11=}}
657657
}
658658
}
659659

test/Concurrency/reasync.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func asyncFunction() async {}
4242

4343
func callReasyncFunction() async {
4444
reasyncFunction { }
45-
await reasyncFunction { } // expected-warning {{no 'async' operations occur within 'await' expression}}
45+
await reasyncFunction { } // expected-warning {{no 'async' operations occur within 'await' expression}}{{3-9=}}
4646

4747
reasyncFunction { await asyncFunction() }
4848
// expected-error@-1:3 {{expression is 'async' but is not marked with 'await'}}{{3-3=await }}
@@ -58,11 +58,11 @@ enum HorseError : Error {
5858
func callReasyncRethrowsFunction() async throws {
5959
reasyncRethrowsFunction { }
6060
await reasyncRethrowsFunction { }
61-
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}
61+
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}{{3-9=}}
6262
try reasyncRethrowsFunction { }
6363
// expected-warning@-1 {{no calls to throwing functions occur within 'try' expression}}
6464
try await reasyncRethrowsFunction { }
65-
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}
65+
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}{{7-13=}}
6666
// expected-warning@-2 {{no calls to throwing functions occur within 'try' expression}}
6767

6868
reasyncRethrowsFunction { await asyncFunction() }
@@ -84,10 +84,10 @@ func callReasyncRethrowsFunction() async throws {
8484
await reasyncRethrowsFunction { throw HorseError.colic }
8585
// expected-error@-1 {{call can throw but is not marked with 'try'}}
8686
// expected-note@-2 {{call is to 'rethrows' function, but argument function can throw}}
87-
// expected-warning@-3 {{no 'async' operations occur within 'await' expression}}
87+
// expected-warning@-3 {{no 'async' operations occur within 'await' expression}}{{3-9=}}
8888
try reasyncRethrowsFunction { throw HorseError.colic }
8989
try await reasyncRethrowsFunction { throw HorseError.colic }
90-
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}
90+
// expected-warning@-1 {{no 'async' operations occur within 'await' expression}}{{7-13=}}
9191

9292
reasyncRethrowsFunction { await asyncFunction(); throw HorseError.colic }
9393
// expected-error@-1 {{call can throw but is not marked with 'try'}}

test/Concurrency/toplevel/no-async-5-top-level.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func isolatedSync() { // expected-note 2 {{calls to global function 'isolatedSyn
1919
}
2020

2121
func nonIsolatedAsync() async {
22-
await print(a) // expected-warning {{no 'async' operations occur within 'await' expression}}
22+
await print(a) // expected-warning {{no 'async' operations occur within 'await' expression}}{{5-11=}}
2323
a = a + 10
2424
}
2525

test/Concurrency/transfernonsendable_rbi_result.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ func testActorCrossingBoundary() async {
7272
let _ = await (await mainActorResult(1))(2)
7373
// expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from main actor-isolated global function 'mainActorResult' to global actor 'CustomActor'-isolated context}}
7474
// expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
75-
// expected-warning @-3 {{no 'async' operations occur within 'await' expression}}
75+
// expected-warning @-3 {{no 'async' operations occur within 'await' expression}}{{11-17=}}
7676

7777
let calc = Calculator()
7878

7979
let _ = (await calc.addCurried(1))(2)
8080
// expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from actor-isolated instance method 'addCurried' to global actor 'CustomActor'-isolated context}}
8181
// expected-note@-2{{a function type must be marked '@Sendable' to conform to 'Sendable'}}
82-
let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}
82+
let _ = await (await calc.addCurried(1))(2) // expected-warning{{no 'async' operations occur within 'await' expression}}{{11-17=}}
8383
// expected-error @-1 {{non-Sendable '(Int) -> Int'-typed result can not be returned from actor-isolated instance method 'addCurried' to global actor 'CustomActor'-isolated context}}
8484
// expected-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
8585

0 commit comments

Comments
 (0)