Hey all,
I have faced following strange behavior of the swift compiler (Xcode 15.3):
class UseCase {
func test() {
Base().foo()
Child().foo()
Child().fooIsolated() // Error: Call to main actor-isolated instance method 'fooIsolated()' in a synchronous nonisolated context
}
}
class Base {
func foo() {
print("Base")
}
}
extension Base: MyProto {}
@MainActor
protocol MyProto {}
class Child: Base {
override func foo() {
print("Child")
}
func fooIsolated() {
print("aa")
}
}
So it seems to me that @MainActor isolated protocol applies to the Childs of the class which is conforming that protocol, but not to that base class. So as you can see from the code I have Base
class and MyProto
protocol which is marked as MainActor
isolated. The Base
class conforms MyProto
protocol with extension. There is also a Child
class which inherits from Base
and overrides the foo
function and adds its own fooIsolated
function. In the UseCase
class where we have non isolated context, the compiler gives an error for calling fooIsolated
function of Child
but not for foo
functions for both Child
and Base
classes. From this I can say that the fooIsolated
function was isolated but the interface inherited from Base
and the superclass itself is not isolated.
Another strange behavior I noticed regarding the warning which we should get in this cases "Main actor-isolated class 'Child' has different actor isolation from nonisolated superclass 'Base'; this is an error in Swift 6". this warning is not fired but it should from my understanding, and it fires only when I explicitly mark Child
as MainActor
or the Child
class was defined in different package than Base
class.
Any ideas on this?
Thanks