Sourcery icon indicating copy to clipboard operation
Sourcery copied to clipboard

The generic type is not recognized correctly

Open ssly1997 opened this issue 2 years ago • 1 comments

struct TestGeneric<T>: JSONDictionaryConvertible where T: JSONDictionaryConvertible {
	let name: String
	let haha: T
}

variable haha conforms protocol JSONDictionaryConvertible but variable haha's type.implements do not include JSONDictionaryConvertible

ssly1997 avatar Aug 16 '22 03:08 ssly1997

Had the same issue, as a workaround I added this to my stencil to get the class / struct generic conformance, definitely not perfect, but it works. Hope this helps you forward :)

{% macro fullAssociatedTypes type %}{% for _, baseType in type.basedTypes where forloop.first %}{% if baseType.associatedTypes %}<{% for _, baseType in type.basedTypes %}{% if not forloop.first %}, {% endif %}{% for _, associatedType in baseType.associatedTypes %}{% if not forloop.first %}, {% endif %}{{ associatedType.name }}: {{ associatedType.typeName.name }}{% endfor %}{% endfor %}>{% endif %}{% endfor %}{% endmacro %}

{% macro shortAssociatedTypes type %}{% for _, baseType in type.basedTypes where forloop.first %}{% if baseType.associatedTypes %}<{% for _, baseType in type.basedTypes %}{% if not forloop.first %}, {% endif %}{% for _, associatedType in baseType.associatedTypes %}{% if not forloop.first %}, {% endif %}{{ associatedType.name }}{% endfor %}{% endfor %}>{% endif %}{% endfor %}{% endmacro %}

{% for type in types.classes %}
struct {{ type.name }}Adapter{% call fullAssociatedTypes type %} {
   let adaptee: {{ type.name }}{% call shortAssociatedTypes type %}
}
{% endfor %}

I should also mention that I only tested this with generic conformance on the left side like this and I haven't tested it on your code yet

struct TestGeneric<T: JSONDictionaryConvertible>: JSONDictionaryConvertible { }

stefanrenne avatar Aug 19 '22 07:08 stefanrenne

👋🏻 Hello,

confirmed the bug, minimal reproducible:

template:

{% for struct in types.structs %}
    {% for variable in struct.variables where variable.type.implements.AutoHashable %}
        {{ variable.name }}
    {% endfor %}    
{% endfor %}

input

protocol AutoHashable {}

/// General protocol
protocol AutoHashableProtocol: AutoHashable {
    var width: Double { get }
    var height: Double { get}
    static var name: String { get }
}
struct Testy: AutoHashableProtocol {
    var width: Double
    var height: Double
    static var name: String { "" }
    let name: String
}
struct TestGeneric<T>: AutoHashable where T: AutoHashable {
	let name: String
	let haha: T
    var hoho: Testy
}
Expected Output Actual Output
haha, hoho hoho

art-divin avatar Mar 02 '24 18:03 art-divin