swiftlang / swift

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

[Concurrency] Unexpected error involving `@autoclosure` #75843

Open NachoSoto opened 1 month ago

NachoSoto commented 1 month ago

Description

I'm unable to think of how this could lead to a data race, so I believe it's an invalid diagnostic.

Reproduction

enum Log {
  static func f(_ name: @autoclosure () -> StaticString) async {}
}

@MainActor
class C {
  func f() async {
    // Sending main actor-isolated value of type '() -> StaticString' with later accesses to nonisolated context risks causing data races
    await Log.f("Test")
  }
}

Expected behavior

I would expect this to compile with no problems (like it does on Swift 5).

Actual behavior

Sending main actor-isolated value of type '() -> StaticString' with later accesses to nonisolated context risks causing data races

Environment

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1)
Target: arm64-apple-macosx14.0

Additional information

Adding isolation _: isolated (any Actor)? = #isolation to Log.f fixes the problem, but: a) That's not backwards compatible with Swift 5.x, making it very cumbersome to write code that's compatible with both b) I fail to see why that's necessary in this case.

NachoSoto commented 1 month ago

cc @hborla

hborla commented 1 month ago

I agree, I don't see any reason why this code should produce the invalid send error. Nothing in the auto-closure touches main-actor-isolated state.

gottesmm commented 1 week ago

Taking a look at this. @NachoSoto thank you for the nice test case!