Skip to content

Error when overriding a registration for the main actor #342

@fraserscottmorrison

Description

@fraserscottmorrison

Hi I'm upgrading my SPM Packages to Swift 6.2, and also upgrading Factory to 2.5.3

I'm registering a dependency for the main actor

extension Container {
  @MainActor
  var mainActorType: Factory<MainActorType> {
      self { @MainActor in SomeMainActorType() }
  }
} 

And I have a XCTestCase also for the main actor where I override the registration

@MainActor
final class MyTests: XCTestCase {
private var mockMainActorInstance: MockMainActorType!

override func setUp() async throws {
        Container.shared.mainActorType.register { params in
            self.mockMainActorInstance = MockMainActorType(withParams: params)
            return self.mockMainActorInstance
        }
}
}

But I get the error
Main actor-isolated property 'mockMainActorInstance' can not be referenced from a Sendable closure

Looking at the Factory code it looks like adding @mainactor to the closure parameter could/should resolve this issue

public extension Factory {
    @discardableResult
    func registerMain(factory: @MainActor @escaping () -> T) -> Self {
        self.register(factory: factory)
        return self
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions