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

Incorrect type validation for @Binds #750

Closed
ZacSweers opened this issue Sep 22, 2023 · 0 comments · Fixed by #833
Closed

Incorrect type validation for @Binds #750

ZacSweers opened this issue Sep 22, 2023 · 0 comments · Fixed by #833
Labels
bug Something isn't working

Comments

@ZacSweers
Copy link
Collaborator

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 a String to an Int. 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 of ItemMapper<ItemDetail> because generics are invariant by default.


Notes since the issue on dagger: Dagger correctly catches this, it seems Anvil does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants