Skip to content

Commit

Permalink
Update error message when there are no supertypes for the binding par…
Browse files Browse the repository at this point in the history
…ameter.
  • Loading branch information
JoelWilcox committed Dec 16, 2022
1 parent 3ee77e4 commit 7f2cac3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
}
}
Expand Down

0 comments on commit 7f2cac3

Please sign in to comment.