Skip to content

[Feature] Support unchecked Sendable #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
berbs-fetch opened this issue Apr 15, 2025 · 1 comment · May be fixed by #106
Open
1 task done

[Feature] Support unchecked Sendable #86

berbs-fetch opened this issue Apr 15, 2025 · 1 comment · May be fixed by #106
Assignees
Labels
enhancement New feature or request

Comments

@berbs-fetch
Copy link

berbs-fetch commented Apr 15, 2025

Use Case

Use @Mocked for a protocol that is Sendable but the default generated mock is not Sendable and fails the compiler.

Feature Proposal

I would like to see @Mocked support an interface like this:

@Mocked 
protocol MyInterface: Sendable {
    func myFunction(arg: Any)
}

While this is technically valid, and the implementation will be Sendable, the generated mock is NOT sendable.

The error is something like:

Stored property '__doSomething' of 'Sendable'-conforming class 'TestMock' has non-sendable type '(method: MockVoidMethodWithParameters<Any>, invoke: (Any) -> Void, reset: () -> Void)'

The proposal is to add an argument to the @Mocked macro to include @unchecked in the generated code.

@Mocked(sendability: .unchecked | .checked (default))

Note

Not all protocols implement Sendable and this new argument would only apply if the underlying protocol implements Sendable. That will have to be thought of when analyzing the syntax tree of the protocol being mocked.

Alternatives Considered

  • I shouldn't use type Any and actually have a Sendable type.
    • In my context this would be a pain b/c it is types: NSArray, NSDictionary, NSNull, NSNumber, NSDate. I can't cast these as Sendable b/c the objc types don't implement that protocol.
    • My interface is based on what Apple gives me, and their type is Any
  • Manually implementing the mock using @MockedMembers and @unchecked Sendable
@MockedMembers
final class TestMock: Test, @unchecked Sendable {
    func doSomething(foo: Any)
}

Additional Context

My real life use case is with WebKit.

I have this actual protocol that is converting the type Any to a Sendable type T.

@Mocked 
public protocol WebMessageDecoder: Sendable {
    func decode<T: Decodable & Sendable>(_ type: T.Type, from body: Any) throws -> T
}

Code of Conduct

  • I agree to follow this project's Code of Conduct
@berbs-fetch berbs-fetch added the enhancement New feature or request label Apr 15, 2025
@graycampbell graycampbell self-assigned this Apr 17, 2025
@graycampbell graycampbell changed the title [Feature] Support Unchecked Sendable [Feature] Support unchecked Sendable Apr 23, 2025
@nolinmcfarland nolinmcfarland linked a pull request Apr 27, 2025 that will close this issue
15 tasks
@nolinmcfarland
Copy link
Contributor

Hey @berbs-fetch 👋🏻 I opened PR #106 that adds support for this feature. Feedback is welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants