swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.59k stars 10.37k forks source link

False "No 'async' operations occur within 'await' expression" warning in Xcode 16 #75002

Open PRESIDENT810 opened 4 months ago

PRESIDENT810 commented 4 months ago

Description

I also posted here: https://forums.swift.org/t/calling-actors-method-in-an-async-closure-doesnt-allow-await-now/72898

I think calling methods on actors should be by default async. However here in SomeMethod, calling it requires await, but in SomeProperty compiler complains that there shouldn't be an await (see reproductoin code)

Looks like a bug introduced in Swift 6 compiler.

Reproduction

actor FooManager {
  func performActionA() -> Int{
    return 1
  }
}

struct SomeStruct{
  public func SomeMethod() -> Int{
    let manager = FooManager()
    let _ = { @Sendable () async -> Int in
      return await manager.performActionA()
    }
    return 1
  }

  public var SomeProperty: Int = {
    let manager = FooManager()
    let _ = { @Sendable () async -> Int in
      return await manager.performActionA() // Warning No 'async' operations occur within 'await' expression
    }
    return 1
  }()
}

Expected behavior

No warning emitted (which is exactly what Xcode 15 / Swift 5 does)

Environment

swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10) Target: arm64-apple-macosx14.0

I also tried to build from source with revision 050f057f and it gives the false warning too

Additional information

No response