5

Consider the following code:

class Cat {
    var name = "Tom"
}

class Globals {
    var cat = Cat()
}

let glob = Globals()

func one () {
    Task {glob.cat.name="Max"} // Expected Warning about some nonSendable moving into a different concurrency domain
}

Normally, with -warn-concurrency enabled, Swift/Xcode warns about non-sendables crossing concurrency domains.

In my understanding, the closure which is passed to Task must always be a @Sendable closure. A @Sendable closure can only capture Sendables. However, Globals is not a Sendable type. I excpected a warning along the lines of

Capture of 'glob' with non-sendable type 'Globals' in a @Sendable closure

No such warning is emitted.

JMC
  • 1,723
  • 1
  • 11
  • 20
  • 4
    Almost certainly a compiler bug. The compiler has many limitations in how it can reason about global variables. The short answer is "don't make global mutable variables." It's come up on the forums, but hasn't gotten any discussion. https://forums.swift.org/t/sendability-checking-for-global-variables/56515 I don't see a bug open for it yet, so you may want to do that (bugs.swift.org). – Rob Napier Oct 23 '22 at 14:10

0 Answers0