From 7f2cac3c7af319964d943a6f349593608dcd6d45 Mon Sep 17 00:00:00 2001 From: Joel Wilcox Date: Thu, 15 Dec 2022 16:54:37 -0800 Subject: [PATCH] Update error message when there are no supertypes for the binding parameter. --- .../codegen/dagger/BindsMethodValidator.kt | 9 +++-- .../dagger/BindsMethodValidatorTest.kt | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/BindsMethodValidator.kt b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/BindsMethodValidator.kt index 30e4d279f..67db5c504 100644 --- a/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/BindsMethodValidator.kt +++ b/compiler/src/main/java/com/squareup/anvil/compiler/codegen/dagger/BindsMethodValidator.kt @@ -81,10 +81,15 @@ internal class BindsMethodValidator : PrivateCodeGenerator() { .map { it.shortName } .toList() + val superTypesMessage = if (paramSuperTypes.size == 1) { + "has no supertypes." + } else { + "only has the following supertypes: ${paramSuperTypes.drop(1)}" + } throw AnvilCompilationExceptionFunctionReference( message = "@Binds methods' parameter type must be assignable to the return type. " + - "Expected return type of $returnType but impl parameter of type " + - "${paramSuperTypes.first()} only has the following supertypes: $paramSuperTypes", + "Expected binding of type $returnType but impl parameter of type " + + "${paramSuperTypes.first()} $superTypesMessage", functionReference = function ) } diff --git a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/BindsMethodValidatorTest.kt b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/BindsMethodValidatorTest.kt index 63c369bd5..f64a20c41 100644 --- a/compiler/src/test/java/com/squareup/anvil/compiler/dagger/BindsMethodValidatorTest.kt +++ b/compiler/src/test/java/com/squareup/anvil/compiler/dagger/BindsMethodValidatorTest.kt @@ -55,8 +55,40 @@ class BindsMethodValidatorTest( ) if (!useDagger) { assertThat(messages).contains( - "Expected return type of Bar but impl parameter of type Foo only has the following " + - "supertypes: [Foo, Ipsum, Lorem]" + "Expected binding of type Bar but impl parameter of type Foo only has the following " + + "supertypes: [Ipsum, Lorem]" + ) + } + } + } + + @Test + fun `a binding with an incompatible parameter type with no supertypes fails to compile`() { + compile( + """ + package com.squareup.test + + import dagger.Binds + import dagger.Module + import javax.inject.Inject + + class Foo @Inject constructor() + interface Bar + + @Module + abstract class BarModule { + @Binds + abstract fun bindsBar(impl: Foo): Bar + } + """ + ) { + assertThat(exitCode).isError() + assertThat(messages).contains( + "@Binds methods' parameter type must be assignable to the return type" + ) + if (!useDagger) { + assertThat(messages).contains( + "Expected binding of type Bar but impl parameter of type Foo has no supertypes." ) } }