Skip to content

Commit

Permalink
Fix the bound type error message when there are multiple inherited ty…
Browse files Browse the repository at this point in the history
…pes and actually use the right type parameters.
  • Loading branch information
vRallev committed Aug 27, 2021
1 parent 695b84a commit 910adcf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,16 @@ internal class BindingModuleGenerator(
fun KotlinType.describeTypeParameters(): String = arguments
.ifEmpty { return "" }
.joinToString(prefix = "<", postfix = ">") { typeArgument ->
typeArgument.type.classDescriptorForType().name.asString() +
typeArgument.type.describeTypeParameters()
typeArgument.type.toString() + typeArgument.type.describeTypeParameters()
}

val boundType = type.typeConstructor
.supertypes
.first { it.classDescriptorForType() == boundTypeDescriptor }

throw AnvilCompilationException(
classDescriptor = boundTypeDescriptor,
message = "Binding ${boundTypeDescriptor.fqNameSafe} contains type parameters(s)" +
" ${boundType.describeTypeParameters()}." +
message = "Class ${type.fqNameSafe} binds ${boundTypeDescriptor.fqNameSafe}," +
" but the bound type contains type parameter(s)" +
" ${boundTypeDescriptor.defaultType.describeTypeParameters()}." +
" Type parameters in bindings are not supported. This binding needs" +
" to be contributed to a dagger module manually"
" to be contributed in a Dagger module manually."
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ class BindingModuleGeneratorTest(
class SomeOtherType
@ContributesBinding(Any::class, ParentInterface::class)
@ContributesBinding(Any::class)
interface ContributingInterface :
ParentInterface<Map<String, List<Pair<String, Int>>>, SomeOtherType>
Expand All @@ -387,8 +387,41 @@ class BindingModuleGeneratorTest(

assertThat(messages).contains("Source0.kt: (6, 11)")
assertThat(messages).contains(
"Binding com.squareup.test.ParentInterface contains type parameters(s)" +
" <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
"Class com.squareup.test.ContributingInterface binds com.squareup.test.ParentInterface, " +
"but the bound type contains type parameter(s) <T, S>. Type parameters in bindings " +
"are not supported. This binding needs to be contributed in a Dagger module manually."
)
}
}

@Test fun `the contributed binding class must not have a generic type parameter with super type chain`() {
compile(
"""
package com.squareup.test
import com.squareup.anvil.annotations.ContributesBinding
$import
interface ParentInterface<out OutputT : Any>
interface MiddleInterface<out OutputT : Any> : ParentInterface<OutputT>
class SomeOtherType
@ContributesBinding(Any::class, ParentInterface::class)
interface ContributingInterface : MiddleInterface<SomeOtherType>
$annotation(Any::class)
interface ComponentInterface
"""
) {
assertThat(exitCode).isEqualTo(COMPILATION_ERROR)

assertThat(messages).contains("Source0.kt: (6, 11)")
assertThat(messages).contains(
"Class com.squareup.test.ContributingInterface binds com.squareup.test.ParentInterface, " +
"but the bound type contains type parameter(s) <OutputT>. Type parameters in " +
"bindings are not supported. This binding needs to be contributed in a Dagger module " +
"manually."
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ class BindingModuleMultibindingSetTest(

assertThat(messages).contains("Source0.kt: (6, 11)")
assertThat(messages).contains(
"Binding com.squareup.test.ParentInterface contains type parameters(s)" +
" <Map<String, List<Pair<String, Int>>>, SomeOtherType>"
"Class com.squareup.test.ContributingInterface binds com.squareup.test.ParentInterface, " +
"but the bound type contains type parameter(s) <T, S>. Type parameters in bindings " +
"are not supported. This binding needs to be contributed in a Dagger module manually."
)
}
}
Expand Down

0 comments on commit 910adcf

Please sign in to comment.