Sourcery icon indicating copy to clipboard operation
Sourcery copied to clipboard

Sourcery doesn't properly work when using protocols with primary associated types

Open jgongo opened this issue 6 months ago • 1 comments

Sourcery doesn't seem to be able to properly generate code when using protocols with primary associated types. The problem arises when you have a protocol that inherits from another protocol with primary associated types, using an argument type for the inherited protocol:

protocol BaseProtocol<Element> {
    associatedtype Element
    var elements: Array<Element> { get }
}

// sourcery:AutoMockable
protocol ChildProtocol: BaseProtocol<String> {
    var count: Int { get }
}

Running this with the standard mock template produces the following output, missing the elements property defined in the parent protocol:

class ChildProtocolMock: ChildProtocol {
    var count: Int {
        get { return underlyingCount }
        set(value) { underlyingCount = value }
    }
    var underlyingCount: (Int)!
}

The problem seems to be related with the inheritance clause. If you instead declare the second protocol in the following way:

protocol ChildProtocol: BaseProtocol where Element == String {
    var count: Int { get }
}

you get the following output, now including the property defined in the parent protocol:

class ChildProtocolMock: ChildProtocol {
    var count: Int {
        get { return underlyingCount }
        set(value) { underlyingCount = value }
    }
    var underlyingCount: (Int)!
    var elements: Array<Element> = []
}

jgongo avatar Jan 29 '24 11:01 jgongo

👋 Hello @jgongo ;

thank you very much for a detailed description and for filing the issue!

Nice find, should be easy to fix for 2.1.8 release.

art-divin avatar Jan 29 '24 11:01 art-divin

As of the latest master branch, I cannot reproduce this bug. Might have been resolved as a consequence.

art-divin avatar Mar 04 '24 19:03 art-divin