swiftlang / swift

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

Region isolation diagnoses async iterator transfer despite `nonisolated(unsafe)` #72942

Open hborla opened 6 months ago

hborla commented 6 months ago

Description

No response

Reproduction

@MainActor
func iterate(over stream: AsyncStream<Int>) async {
  for await ns in stream {
    print(ns)
  }
}

Building this code with -strict-concurrency=complete results in

warning: transferring '$ns$generator' may cause a race; this is an error in the Swift 6 language mode
  4 | @MainActor
  5 | func iterate(over stream: AsyncStream<Int>) async {
  6 |   for await ns in stream {
    |   |- warning: transferring '$ns$generator' may cause a race; this is an error in the Swift 6 language mode
    |   |- note: transferring disconnected '$ns$generator' to nonisolated callee could cause races in between callee nonisolated and local main actor-isolated uses
    |   `- note: use here could race
  7 |     print(ns)
  8 |   }

Expected behavior

No warning. The constraint system inserts nonisolated(unsafe) on the iterator variable to suppress this issue per https://github.com/apple/swift/pull/70697.

Environment

Swift version 6.0-dev (LLVM 401f5becfe44b89, Swift 129a021e790bc26)

Additional information

No response

gottesmm commented 6 months ago

So I looked at this. I basically need to smuggle a bit.

vanvoorden commented 3 months ago

I'm no longer seeing this warning from Xcode_16_beta_2 and Swift 6.

moreindirection commented 3 months ago

I'm still seeing this in Xcode 16 beta 2.