Sendability checking for global variables

Hi, I was experimenting with Sendable using Swift Development Snapshot 2022-03-22 and -enable-actor-data-race-checks flag.

Does Swift check sendability for global variables? see an example:

// Non-Sendable
class MyClass {
    var value: Int = 0

// Global
let otherClass = OtherClass()

class OtherClass {
    var myClassOutside: MyClass!

actor MyActor {
    // Non-Sendable actor-isolated instance of MyClass
    var myClass: MyClass = .init()
    func doSomethingInside() {
        // Save reference to non-Sendable actor-isolated myClass in global otherClass
        // No warnings about MyClass being Non-Sendable
        // but I think it is ok, there is no problem until we use it outside this actor concurrency domain?
        otherClass.myClassOutside = myClass
        Task.detached {
            // mutating MyClass concurrently in Task.detached
            // No warnings about MyClass being Non-Sendable
            otherClass.myClassOutside.value = 6

Task.detached does not capture otherClass because it is global and because of that slips through sendability checking?

Thanks :)

1 Like