swiftlang / swift

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

[Concurrency] Task created from local variable `didSet` leads to `dispatch_assert_queue_fail` #76068

Open NachoSoto opened 2 weeks ago

NachoSoto commented 2 weeks ago

Description

image

Reproduction

struct Data: Sendable {
  var n: Int = 0

  mutating func modify() {
    n += 1
  }
}

@MainActor
class C {
  private var current: Data = .init()

  func f() {
    Task.detached { [self] in
      var data = Data() {
        didSet {
          Task { @MainActor [data] in
            // Should be called in the main thread, but `dispatch_assert_queue_fail`
            current = data
          }
        }
      }

      data.modify()
    }
  }
}

C().f()

Expected behavior

Task is called on main actor.

Environment

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
Target: arm64-apple-macosx14.0

Additional information

No response

NachoSoto commented 2 weeks ago

cc @hborla