Repository: evant/kotlin-inject
Pull request #38 (2020-08-15 15:38:22)

Work around but in ksp where isAssignableFrom doesn't work with typealises


Patch:
diff --git a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
index 1a62582..c496586 100644
--- a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
+++ b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
@@ -407,8 +407,8 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
 
     private fun Context.findWithIndex(key: TypeKey, index: Int, size: Int): Result? {
         val indexFromEnd = size - index - 1
-        args.asReversed().forEachIndexed { i, (type, name) ->
-            if (i == indexFromEnd && type.isAssignableFrom(key.type)) {
+        args.asReversed().getOrNull(indexFromEnd)?.let { (type, name) ->
+            if (type.isAssignableFrom(key.type)) {
                 return Result.Arg(name)
             }
         }
diff --git a/kotlin-inject-compiler-ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/KSAst.kt b/kotlin-inject-compiler-ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/KSAst.kt
index d904cb7..00bab06 100644
--- a/kotlin-inject-compiler-ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/KSAst.kt
+++ b/kotlin-inject-compiler-ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/KSAst.kt
@@ -312,7 +312,7 @@ private class KSAstType(provider: KSAstProvider) : AstType(), KSAstAnnotated, KS
     }
 
     override fun isFunction(): Boolean {
-        val name = actualType().qualifiedName ?: return false
+        val name = type.actualType().declaration.qualifiedName ?: return false
         return name.getQualifier() == "kotlin" && name.getShortName().matches(Regex("Function[0-9]+"))
     }
 
@@ -347,7 +347,9 @@ private class KSAstType(provider: KSAstProvider) : AstType(), KSAstAnnotated, KS
 
     override fun isAssignableFrom(other: AstType): Boolean {
         require(other is KSAstType)
-        return type.isAssignableFrom(other.type)
+        // potential bug in isAssignableFrom when using typealises, manually check their underlying type
+        // https://github.com/android/kotlin/issues/106
+        return type.actualType().isAssignableFrom(other.type.actualType())
     }
 
     override fun equals(other: Any?): Boolean {
@@ -359,9 +361,9 @@ private class KSAstType(provider: KSAstProvider) : AstType(), KSAstAnnotated, KS
         return asTypeName().hashCode()
     }
 
-    private fun actualType() = when (val declaration = declaration) {
-        is KSTypeAlias -> declaration.findActualType()
-        else -> declaration
+    private fun KSType.actualType(): KSType = when (val declaration = declaration) {
+        is KSTypeAlias -> declaration.type.resolve()!!.actualType()
+        else -> this
     }
 }
 


Test patch:
diff --git a/integration-tests/src/test/kotlin/me/tatarka/inject/test/InjectFunctionTest.kt b/integration-tests/src/test/kotlin/me/tatarka/inject/test/InjectFunctionTest.kt
index dea63ed..3824a28 100644
--- a/integration-tests/src/test/kotlin/me/tatarka/inject/test/InjectFunctionTest.kt
+++ b/integration-tests/src/test/kotlin/me/tatarka/inject/test/InjectFunctionTest.kt
@@ -10,10 +10,12 @@ import org.junit.Test
     abstract val bar: bar
 }
 
-typealias foo = (String) -> String
+typealias F = String
+
+typealias foo = (F) -> String
 
 @Inject
-fun foo(dep: Foo, arg: String): String = arg
+fun foo(dep: Foo, arg: F): String = arg
 
 typealias bar = () -> String