square / anvil

A Kotlin compiler plugin to make dependency injection with Dagger 2 easier.
Apache License 2.0
1.29k stars 76 forks source link

Strange behavior: @Binds methods' parameter type must be assignable to the return type. #885

Open SteinerOk opened 4 months ago

SteinerOk commented 4 months ago

First of all, I want to say that I have read this fix (https://github.com/square/anvil/pull/833) and understand the situation described below.

I have a set of custom code generators, before version 2.5.0-beta02 they ran fine and were used in a working project, but after updating to 2.5.0-beta02 I get a compilation error:

e: com.squareup.anvil.compiler.api.AnvilCompilationException: Back-end (JVM) Internal error: @Binds methods' parameter type must be assignable to the return type. Expected binding of type dev.steinerok.sealant.work.WorkerAssistedFactory<out androidx.work.ListenableWorker> but impl parameter of type dev.steinerok.sealant.sample.feature.home.FeatureHomeWorker_AssistedFactory only has the following supertypes: [dev.steinerok.sealant.work.WorkerAssistedFactory<dev.steinerok.sealant.sample.feature.home.FeatureHomeWorker>]
File being compiled: (36,3) in //Users/steinerok/Documents/Projects/sealant/sample/feature-home/build/anvil/debug/generated/dev/steinerok/sealant/sample/feature/home/FeatureHomeWorker_Creation.kt

    at com.squareup.anvil.compiler.internal.reference.FunctionReferenceKt.AnvilCompilationExceptionFunctionReference(FunctionReference.kt:45)
    at com.squareup.anvil.compiler.internal.reference.FunctionReferenceKt.AnvilCompilationExceptionFunctionReference$default(FunctionReference.kt:40)
    at com.squareup.anvil.compiler.codegen.dagger.BindsMethodValidator.validateBindsFunction(BindsMethodValidator.kt:117)
    at com.squareup.anvil.compiler.codegen.dagger.BindsMethodValidator.checkCode(BindsMethodValidator.kt:72)
    at com.squareup.anvil.compiler.codegen.CheckOnlyCodeGenerator.generateCodePrivate(CheckOnlyCodeGenerator.kt:18)
    at com.squareup.anvil.compiler.codegen.PrivateCodeGenerator.generateCode(PrivateCodeGenerator.kt:20)
    at com.squareup.anvil.compiler.codegen.CodeGenerationExtension.generateCode$generateCode(CodeGenerationExtension.kt:198)
    at com.squareup.anvil.compiler.codegen.CodeGenerationExtension.generateCode(CodeGenerationExtension.kt:236)
    at com.squareup.anvil.compiler.codegen.CodeGenerationExtension.analysisCompleted(CodeGenerationExtension.kt:127)

BUT the point is that it only occurs when you use:

anvil {
    generateDaggerFactories.set(true)
}

If I don't use Anvil generateDaggerFactories, but use KAPT instead, everything compiles fine.

It seems to me that the behavior should be the same in both cases?

To reproduse repo: https://github.com/SteinerOk/sealant/tree/ik/sample Compile fine: assembleDebug in :sample:feature-home-kapt Compile error: assembleDebug in :sample:feature-home

RBusarow commented 4 months ago

Thanks for the report!

There was a change to how we validate binding parameters, and the new logic isn't handling generics properly. I'll get a fix out today.