Global actor isolation doesn't recognize shared actor instance as isolated?

A few comments:

  1. The idiomatic way is to define act() as a global funciton and apply @A to it. This should resolves the error in f().

  2. The "nonisolated context" in the diagnostic in g() appears to be a bug unrelated to assumeIsolated. I've filed a bug.

  3. The issue with g() can be demonstrated with simpler code. IIUC this is as expected because compiler see the parameter of the closure as an instance of A, which isn't necessarily A.share. I think you have to use the solution in this post to get the behavior you wanted.

@globalActor
actor A: GlobalActor {
    static let shared: A = .init()
}

@A
class C {
    var str = "str"

    func g() {
        let fn: (isolated A) throws -> Void = { a in 
            _ = self.str // error: global actor 'A'-isolated property 'str' can not be referenced from a nonisolated context
        }
    }
}
1 Like