Incorrect type validation for `@Binds`
ZacSweers opened this issue · 0 comments
Porting from google/dagger#4065
I was working on a lint and questioning if I understood
PsiType.isAssignableFrom
correctly.The lint is to validate that a dagger
@Binds
function is correctly binding compatible types. For example, it will show a lint error in the IDE if someone tries to bind aString
to anInt
. Obviously Dagger checks this at compile-time, but a lint is nice to get instant feedback in the IDE.There's a complex case I ran into though that has me wondering
Given these types
sealed interface ItemDetail { object DetailTypeA : ItemDetail } interface ItemMapper<T : ItemDetail> class DetailTypeAItemMapper : ItemMapper<ItemDetail.DetailTypeA>We have a bind function like so
@Module interface SomeModule { @Binds fun validComplexBinding(real: DetailTypeAItemMapper): ItemMapper<ItemDetail> }The lint check correctly reports the return type as not assignable from the bound instance. Dagger's compiler however accepts it as valid, and presumably in generated code does an unsafe cast that happens to work at runtime due to erased generics.
I wanted to raise this up as a possible issue, as I think Dagger maybe incorrectly allowing this and the lint check (via PSI) is correctly pointing out that
DetailTypeAItemMapper
isn't a subtype ofItemMapper<ItemDetail>
because generics are invariant by default.
Notes since the issue on dagger: Dagger correctly catches this, it seems Anvil does not.