Skip to content

Required await in default property initializer produces no_async_in_await warning #80624

Open
@danlshields

Description

@danlshields

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.concurrencyFeature: umbrella label for concurrency language features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions