Skip to content

Commit e72dd9b

Browse files
eygraberevant
authored andcommitted
Don't crash if the root of a trace is a TypeParameter
1 parent dc3eba2 commit e72dd9b

File tree

2 files changed

+35
-0
lines changed
  • ast/ksp/src/main/kotlin/me/tatarka/kotlin/ast
  • kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test

2 files changed

+35
-0
lines changed

ast/ksp/src/main/kotlin/me/tatarka/kotlin/ast/KSAst.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.google.devtools.ksp.symbol.KSFunctionDeclaration
2121
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
2222
import com.google.devtools.ksp.symbol.KSType
2323
import com.google.devtools.ksp.symbol.KSTypeAlias
24+
import com.google.devtools.ksp.symbol.KSTypeParameter
2425
import com.google.devtools.ksp.symbol.KSTypeReference
2526
import com.google.devtools.ksp.symbol.KSValueParameter
2627
import com.google.devtools.ksp.symbol.Modifier
@@ -530,6 +531,8 @@ private class KSAstType private constructor(
530531
// we check for error first because KotlinPoet will throw an exception
531532
return if (type.isError) {
532533
typeRef.toString()
534+
} else if (type.declaration is KSTypeParameter) {
535+
typeRef.toString()
533536
} else {
534537
typeRef.toTypeName().toString()
535538
}.shortenPackage()

kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/FailureTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,4 +1273,36 @@ class FailureTest {
12731273
contains("Qualifier: @MyQualifier can only be applied to the outer type")
12741274
}
12751275
}
1276+
1277+
@ParameterizedTest
1278+
@EnumSource(Target::class)
1279+
fun fails_if_cannot_find_inject_or_provider_and_root_is_a_type_parameter(target: Target) {
1280+
val projectCompiler = ProjectCompiler(target, workingDir)
1281+
1282+
assertFailure {
1283+
projectCompiler.source(
1284+
"MyComponent.kt",
1285+
"""
1286+
import me.tatarka.inject.annotations.Component
1287+
import me.tatarka.inject.annotations.Inject
1288+
import me.tatarka.inject.annotations.Provides
1289+
1290+
class Foo
1291+
1292+
@Inject class Bar(val foo: Foo)
1293+
1294+
interface DestinationComponent<C> {
1295+
val c: C
1296+
}
1297+
1298+
@Inject class CType(val bar: Bar)
1299+
1300+
@Component
1301+
abstract class FooBarComponent : DestinationComponent<CType>
1302+
""".trimIndent()
1303+
).compile()
1304+
}.output().all {
1305+
contains("e: [ksp] Cannot find an @Inject constructor or provider for: Foo")
1306+
}
1307+
}
12761308
}

0 commit comments

Comments
 (0)