Open
Description
Description
Awaiting an actor property in a default property initializer produces a warning about no Async operation being performed in the await. The same code without the await does not compile due to an error (which is expected).
Reproduction
actor SomeActor {
var foo: Int? = nil
}
struct SomeStruct {
let bar: Int = {
let myActor = SomeActor()
func foobar() async {
guard let foo = await myActor.foo else {
return
}
print("Foo: \(foo)")
}
return 0
}()
static func initBar() -> Int {
let myActor = SomeActor()
func foobar() async {
guard let foo = await myActor.foo else {
return
}
print("Foo: \(foo)")
}
return 0
}
}
Compiled using
swiftc -emit-module ActorIsolation.swift
Produces this output:
ActorIsolation.swift:10:29: warning: no 'async' operations occur within 'await' expression
8 |
9 | func foobar() async {
10 | guard let foo = await myActor.foo else {
| `- warning: no 'async' operations occur within 'await' expression
11 | return
12 | }
But removing the await
produces this output:
ActorIsolation.swift:10:29: error: expression is 'async' but is not marked with 'await'
8 |
9 | func foobar() async {
10 | guard let foo = myActor.foo else {
| |- error: expression is 'async' but is not marked with 'await'
| `- note: property access is 'async'
11 | return
12 | }
Note that initBar
does not produce the warning diagnostic, only the default property initializer does.
Expected behavior
I expect this example to compile without warnings.
Environment
swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0
Additional information
This seems related to #73892. This Swift Forums thread helped me narrow down the issue.