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

Compilation fix for a mock where the return type of a closure is an opaque type #259

Merged

Conversation

omarzl
Copy link
Contributor

@omarzl omarzl commented Apr 26, 2024

The following code:

protocol ProtocolReturningOpaqueTypeInClosureProtocol {
    func register(router: @autoclosure @escaping () -> (some Error))
}

produces this mock:

class ProtocolReturningOpaqueTypeInClosureProtocolMock: ProtocolReturningOpaqueTypeInClosureProtocol {
    init() { }
    private(set) var registerCallCount = 0
    var registerHandler: ((@autoclosure @escaping () -> (some Error)) -> ())?
    func register(router: @autoclosure @escaping () -> (some Error))  {
        registerCallCount += 1
        if let registerHandler = registerHandler {
            registerHandler(router())
        }
    }
}

causing the compilation error:
'some' cannot appear in parameter position in result type '((@autoclosure @escaping () -> (some Error)) -> ())?'

This pr fixes the variable registerHandler by generating the following mock:

class ProtocolReturningOpaqueTypeInClosureProtocolMock: ProtocolReturningOpaqueTypeInClosureProtocol {
    init() { }
    private(set) var registerCallCount = 0
    var registerHandler: ((@autoclosure @escaping () -> (any Error)) -> ())?
    func register(router: @autoclosure @escaping () -> (some Error))  {
        registerCallCount += 1
        if let registerHandler = registerHandler {
            registerHandler(router())
        }
    }
}
@CLAassistant
Copy link

CLAassistant commented Apr 26, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

I wonder if this implementation would work well with the following code?🤔

protocol P {
     func f(_: () -> (MyAwesomeType))
}
@omarzl
Copy link
Contributor Author

omarzl commented Apr 29, 2024

I wonder if this implementation would work well with the following code?🤔

Hello, I am only replacing the word some to any when the return type is some MyType

These other examples work well:

protocol Foo {
    func register(router: () -> (Bar))
    func register<T>(router: () -> (T))
}

protocol Bar {}

The generated mocks are:

var registerRouterHandler: ((() -> (Bar)) -> ())?
var registerRouterTHandler: ((() -> (Any)) -> ())?
Copy link
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

I've confirmed that MyAwesomeType changes to MyAweanyType.
It seems sufficient to just change the replacement logic to s/some /any /g.

@omarzl
Copy link
Contributor Author

omarzl commented Apr 30, 2024

oh right, sorry I misunderstood you
I updated the fix and the tests to consider this scenario, thanks!

Copy link
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

Maybe work with most codes. Thanks!

@sidepelican
Copy link
Collaborator

Sorry I forgot launch workflow.

@sidepelican sidepelican merged commit dff67bf into uber:master May 2, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants