Fix KSTypeVarianceResolver for suspend functions in type arguments
This CL fixes [KSTypeVarianceResolver] to handle suspend functions in
type arguments, e.g. `List<suspend () -> Unit>`.
I've also added tests to make sure this handles more complicated cases,
e.g. suspend functions nested within other suspend functions.
Test: KspTypeNamesGoldenTest.kt
Change-Id: If0f06e5e8317bdbf48b76af742c6443a82951043
diff --git a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
index 338d041..e676e72 100644
--- a/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
+++ b/room/room-compiler-processing/src/main/java/androidx/room/compiler/processing/ksp/KSTypeVarianceResolver.kt
@@ -118,15 +118,16 @@
}
private fun KSTypeWrapper.replaceSuspendFunctionTypes(): KSTypeWrapper {
+ val newArguments = arguments.map { it.replaceSuspendFunctionTypes() }
return if (!newType.isSuspendFunctionType) {
- this
+ replace(newArguments)
} else {
val newKSType = newType.replaceSuspendFunctionTypes(resolver)
val newType = KSTypeWrapper(resolver, newKSType)
replaceType(newKSType).replace(
buildList {
- addAll(arguments.dropLast(1))
- val originalArg = arguments.last()
+ addAll(newArguments.dropLast(1))
+ val originalArg = newArguments.last()
val continuationArg = newType.arguments[newType.arguments.lastIndex - 1]
add(
continuationArg.replace(
@@ -144,6 +145,11 @@
}
}
+ private fun KSTypeArgumentWrapper.replaceSuspendFunctionTypes(): KSTypeArgumentWrapper {
+ val type = type ?: return this
+ return replace(type.replaceSuspendFunctionTypes(), variance)
+ }
+
private fun KSTypeWrapper.resolveWildcards(
scope: KSTypeVarianceResolverScope?
) = if (scope == null) {
diff --git a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/ksp/KspTypeNamesGoldenTest.kt b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/ksp/KspTypeNamesGoldenTest.kt
index 2cf13ff..db85cce 100644
--- a/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/ksp/KspTypeNamesGoldenTest.kt
+++ b/room/room-compiler-processing/src/test/java/androidx/room/compiler/processing/ksp/KspTypeNamesGoldenTest.kt
@@ -181,6 +181,7 @@
typealias MySuspendLambdaAlias5 = suspend (List<MyGenericIn<@JSW MyGenericOut<MyGenericOut<MyType>>>>) -> List<MyGenericIn<@JSW MyGenericOut<MyGenericOut<MyType>>>>
typealias MySuspendLambdaAlias6 = suspend (List<MyGenericIn<MyGenericOut<@JSW MyGenericOut<MyType>>>>) -> List<MyGenericIn<MyGenericOut<@JSW MyGenericOut<MyType>>>>
typealias MySuspendLambdaAlias7 = suspend (List<MyGenericIn<MyGenericOut<MyGenericOut<@JSW MyType>>>>) -> List<MyGenericIn<MyGenericOut<MyGenericOut<@JSW MyType>>>>
+ typealias MySuspendLambdaAlias8 = List<suspend (List<MyGenericIn<MyGenericOut<MyGenericOut<@JSW MyType>>>>) -> List<MyGenericIn<MyGenericOut<MyGenericOut<@JSW MyType>>>>>
typealias JSW = JvmSuppressWildcards
typealias JW = JvmWildcard
typealias MyLambdaTypeAlias = (@JvmWildcard MyType) -> @JvmWildcard MyType
@@ -378,6 +379,18 @@
fun method58(
param: MyGeneric<out MyGenericOut<MyGenericOut<MyInterface>>>
): MyGeneric<out MyGenericOut<MyGenericOut<MyInterface>>> = TODO()
+ fun method59(
+ param: MyGeneric<suspend () -> Unit>
+ ): MyGeneric<suspend () -> Unit> = TODO()
+ fun method60(
+ param: MyGenericIn<suspend () -> Unit>
+ ): MyGenericIn<suspend () -> Unit> = TODO()
+ fun method61(
+ param: MyGenericOut<suspend () -> Unit>
+ ): MyGenericOut<suspend () -> Unit> = TODO()
+ fun method62(
+ param: MyGenericOut<suspend (MyGenericOut<suspend () -> Unit>) -> MyGenericOut<suspend () -> Unit>>
+ ): MyGenericOut<suspend (MyGenericOut<suspend () -> Unit>) -> MyGenericOut<suspend () -> Unit>> = TODO()
}
""".trimIndent()
), listOf("Subject")
@@ -570,6 +583,7 @@
fun suspendLambda5(param: MySuspendLambdaAlias5): MySuspendLambdaAlias5 = TODO()
fun suspendLambda6(param: MySuspendLambdaAlias6): MySuspendLambdaAlias6 = TODO()
fun suspendLambda7(param: MySuspendLambdaAlias7): MySuspendLambdaAlias7 = TODO()
+ fun suspendLambda8(param: MySuspendLambdaAlias8): MySuspendLambdaAlias8 = TODO()
@JSW fun suspendLambda1WithJSW(param: MySuspendLambdaAlias1): MySuspendLambdaAlias1 = TODO()
@JSW fun suspendLambda2WithJSW(param: MySuspendLambdaAlias2): MySuspendLambdaAlias2 = TODO()
@JSW fun suspendLambda3WithJSW(param: MySuspendLambdaAlias3): MySuspendLambdaAlias3 = TODO()
@@ -577,6 +591,7 @@
@JSW fun suspendLambda5WithJSW(param: MySuspendLambdaAlias5): MySuspendLambdaAlias5 = TODO()
@JSW fun suspendLambda6WithJSW(param: MySuspendLambdaAlias6): MySuspendLambdaAlias6 = TODO()
@JSW fun suspendLambda7WithJSW(param: MySuspendLambdaAlias7): MySuspendLambdaAlias7 = TODO()
+ @JSW fun suspendLambda8WithJSW(param: MySuspendLambdaAlias8): MySuspendLambdaAlias8 = TODO()
}
""".trimIndent()
), listOf(className)