Fix KType.javaType for suspend function return type

#KT-26293 Fixed
This commit is contained in:
Alexander Udalov
2018-08-29 18:23:40 +02:00
parent fba539debf
commit ac14c5e74f
7 changed files with 62 additions and 1 deletions
@@ -0,0 +1,16 @@
// IGNORE_BACKEND: JS, NATIVE, JS_IR, JVM_IR
// WITH_REFLECT
import kotlin.reflect.jvm.javaType
import kotlin.test.assertEquals
interface A {
suspend fun f(param: String): MutableList<in String>
}
fun box(): String {
val type = A::class.members.single { it.name == "f" }.returnType.javaType
assertEquals("java.util.List<? super java.lang.String>", type.toString())
return "OK"
}
@@ -18771,6 +18771,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/reflection/mapping/types/supertypes.kt");
}
@TestMetadata("suspendFun.kt")
public void testSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/suspendFun.kt");
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");
@@ -18771,6 +18771,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/reflection/mapping/types/supertypes.kt");
}
@TestMetadata("suspendFun.kt")
public void testSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/suspendFun.kt");
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");
@@ -18771,6 +18771,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/reflection/mapping/types/supertypes.kt");
}
@TestMetadata("suspendFun.kt")
public void testSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/suspendFun.kt");
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");
@@ -6,9 +6,12 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.WildcardType
import java.util.*
import kotlin.coroutines.Continuation
import kotlin.reflect.*
@@ -63,7 +66,9 @@ internal abstract class KCallableImpl<out R> : KCallable<R> {
get() = _parameters()
private val _returnType = ReflectProperties.lazySoft {
KTypeImpl(descriptor.returnType!!) { caller.returnType }
KTypeImpl(descriptor.returnType!!) {
extractContinuationArgument() ?: caller.returnType
}
}
override val returnType: KType
@@ -191,4 +196,19 @@ internal abstract class KCallableImpl<out R> : KCallable<R> {
else -> throw UnsupportedOperationException("Unknown primitive: $type")
}
} else null
private fun extractContinuationArgument(): Type? {
if ((descriptor as? FunctionDescriptor)?.isSuspend == true) {
// kotlin.coroutines.Continuation<? super java.lang.String>
val continuationType = caller.parameterTypes.lastOrNull() as? ParameterizedType
if (continuationType?.rawType == Continuation::class.java) {
// ? super java.lang.String
val wildcard = continuationType.actualTypeArguments.single() as? WildcardType
// java.lang.String
return wildcard?.lowerBounds?.first()
}
}
return null
}
}
@@ -16871,6 +16871,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/reflection/mapping/types/supertypes.kt");
}
@TestMetadata("suspendFun.kt")
public void testSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/suspendFun.kt");
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");
@@ -17936,6 +17936,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/reflection/mapping/types/supertypes.kt");
}
@TestMetadata("suspendFun.kt")
public void testSuspendFun() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/suspendFun.kt");
}
@TestMetadata("topLevelFunctions.kt")
public void testTopLevelFunctions() throws Exception {
runTest("compiler/testData/codegen/box/reflection/mapping/types/topLevelFunctions.kt");