Skip to content
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

Support Sendable conformed protocol #133

Merged

Conversation

yoshiki-tsukada
Copy link
Contributor

@yoshiki-tsukada yoshiki-tsukada commented Dec 21, 2024

Hello! I fixed an error which occurs in my project.

The following code causes an error on Swift 6.

@Spyable
protocol XxxProtocol: Sendable {
    func fetch() async throws -> [String]
}

class XxxProtocolSpy: XxxProtocol { // ❌ Non-final class 'XxxProtocolSpy' cannot conform to 'Sendable'; use '@unchecked Sendable'
    var fetchCallsCount = 0 // ❌ Stored property 'fetchCallsCount' of 'Sendable'-conforming class 'XxxProtocolSpy' is mutable
    var fetchCalled: Bool {
        return fetchCallsCount > 0
    }
    var fetchThrowableError: (any Error)?
    var fetchReturnValue: [String]!
    var fetchClosure: (() async throws -> [String])?
    func fetch() async throws -> [String] {
        fetchCallsCount += 1
        if let fetchThrowableError {
            throw fetchThrowableError
        }
        if fetchClosure != nil {
            return try await fetchClosure!()
        } else {
            return fetchReturnValue
        }
    }
}

To solve this error, I added @unchecked Sendable conformance to all Spy classes.

Sendable conformed protocol:

protocol XxxProtocol: Sendable {
    func fetch() async throws -> [String]
}

- class XxxProtocolSpy: XxxProtocol {
+ class XxxProtocolSpy: XxxProtocol, @unchecked Sendable {
...
}

not Sendable conformed protocol:

protocol XxxProtocol {
    func fetch() async throws -> [String]
}

- class XxxProtocolSpy: XxxProtocol {
+ class XxxProtocolSpy: XxxProtocol, @unchecked Sendable {
...
}

Copy link

codecov bot commented Dec 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.20%. Comparing base (c81a4c3) to head (8ea114a).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #133   +/-   ##
=======================================
  Coverage   97.20%   97.20%           
=======================================
  Files          21       21           
  Lines        1072     1075    +3     
=======================================
+ Hits         1042     1045    +3     
  Misses         30       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@Matejkob Matejkob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you for your contribution!

@Matejkob Matejkob merged commit f6b90c8 into Matejkob:main Dec 24, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support @unchecked Sendable
2 participants