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.
let glob = Globals()
withvar glob = Globals()
, the compiler will warn you that “Reference to var 'glob' is not concurrency-safe because it involves shared mutable state”, butlet glob = Globals()
seems like it also is not concurrency-safe and has a shared mutable state, too (because it is a reference type). I get that this is a big ask of a compiler, but doesn’t feel right. – Ifni