Conforming non-actor to Actor protocol

It appears possible to conform a non-actor type to the Actor protocol in an extension. For example, the following code compiles successfully:

extension NSManagedObjectContext: Actor {
  var unownedExecutor: UnownedSerialExecutor {
    // Return an executor here that executes things on the context
  }
}

(I know this falls under the "don't conform types you don't own to protocols you don't own" banner. I'm not planning to ship this; just exploring.)

What is the impact of this, and should this be allowed?

  • Does this mean that all properties and functions of a managed object context are now isolated? (The compiler doesn't seem to think so; I can still access those properties synchronously from nonisolated contexts.)
  • Does this mean that all async functions will now execute on the given executor?
  • Does this mean that only async functions defined in the module where the extension was declared would execute on the given executor?

The compiler doesn't allow me to declare this on my own class, so this seems to be something that I can only do in extensions to classes from other modules:

// error: Non-actor type 'Person' cannot conform to the 'Actor' protocol
class Person {}

extension Person: Actor {
    var unownedExecutor: UnownedSerialExecutor {
        return MainActor.sharedUnownedExecutor
    }
}
4 Likes

I expect that's a bug… I don't see how it could be expected to actually work. Especially when applied to an Objective-C class.