From 272f6abe69d3348666bd39e3179e1d6556ade709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Mon, 17 Feb 2020 13:33:00 +0100 Subject: [PATCH] JVM IR: Fix suspend lambda generic signatures --- .../jvm/lower/AddContinuationLowering.kt | 2 +- .../suspendFunctionLiteralGenericSignature.kt | 73 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 ++ .../LightAnalysisModeTestGenerated.java | 5 ++ .../ir/FirBlackBoxCodegenTestGenerated.java | 5 ++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 ++ 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt index 80510eae937..ded02ab9df7 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt @@ -164,7 +164,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : functionNClass, hasQuestionMark = false, arguments = (info.function.explicitParameters.subList(0, info.arity).map { it.type } - + info.function.continuationType() + info.function.returnType) + + info.function.continuationType() + context.irBuiltIns.anyNType) .map { makeTypeProjection(it, Variance.INVARIANT) }, annotations = emptyList() ) diff --git a/compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt b/compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt new file mode 100644 index 00000000000..25f5dbbeb88 --- /dev/null +++ b/compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt @@ -0,0 +1,73 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM +// WITH_RUNTIME + +import java.util.Date + +fun assertEquals(expected: String, actual: Any) { + if ("$actual" != expected) + throw AssertionError("Fail, expected: $expected, actual: $actual") +} + +fun assertGenericSuper(expected: String, function: Any?) { + val clazz = (function as java.lang.Object).getClass()!! + val genericSuper = clazz.getGenericInterfaces()[0]!! + assertEquals(expected, genericSuper) +} + +val unitFun = suspend { } +val intFun = suspend { 42 } + +fun assertStringParamFun(lambda: suspend (String) -> Unit) { + assertEquals( + "kotlin.jvm.functions.Function2, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun assertListFun(lambda: suspend (List) -> Unit) { + assertEquals( + "kotlin.jvm.functions.Function2, kotlin.coroutines.Continuation, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun assertMutableListFun(lambda: suspend (MutableList) -> MutableList) { + assertEquals( + "kotlin.jvm.functions.Function2, kotlin.coroutines.Continuation>, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun assertFunWithIn(lambda: suspend (Comparable) -> Unit) { + assertEquals( + "kotlin.jvm.functions.Function2, kotlin.coroutines.Continuation, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun assertExtensionFun(lambda: suspend Any.() -> Unit) { + assertEquals( + "kotlin.jvm.functions.Function2, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun assertExtensionWithArgFun(lambda: suspend Long.(x: Any) -> Date) { + assertEquals( + "kotlin.jvm.functions.Function3, java.lang.Object>", + lambda.javaClass.genericInterfaces.single() + ) +} + +fun box(): String { + assertGenericSuper("kotlin.jvm.functions.Function1, java.lang.Object>", unitFun) + assertGenericSuper("kotlin.jvm.functions.Function1, java.lang.Object>", intFun) + assertStringParamFun { x: String -> } + assertListFun { l: List -> l } + assertMutableListFun { l -> null!! } + assertFunWithIn { x -> } + assertExtensionFun { } + assertExtensionWithArgFun { x -> Date() } + return "OK" +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 697df275aa5..4c2d4def357 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -24420,6 +24420,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testSignatureOfSimpleInnerSimpleOuter() throws Exception { runTest("compiler/testData/codegen/box/reflection/genericSignature/signatureOfSimpleInnerSimpleOuter.kt"); } + + @TestMetadata("suspendFunctionLiteralGenericSignature.kt") + public void testSuspendFunctionLiteralGenericSignature() throws Exception { + runTest("compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt"); + } } @TestMetadata("compiler/testData/codegen/box/reflection/isInstance") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 1e7e5f7b573..04209e36173 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -23237,6 +23237,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes public void testSignatureOfSimpleInnerSimpleOuter() throws Exception { runTest("compiler/testData/codegen/box/reflection/genericSignature/signatureOfSimpleInnerSimpleOuter.kt"); } + + @TestMetadata("suspendFunctionLiteralGenericSignature.kt") + public void testSuspendFunctionLiteralGenericSignature() throws Exception { + runTest("compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt"); + } } @TestMetadata("compiler/testData/codegen/box/reflection/isInstance") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 65b9cfb3ef6..17070056777 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -22924,6 +22924,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT public void testSignatureOfSimpleInnerSimpleOuter() throws Exception { runTest("compiler/testData/codegen/box/reflection/genericSignature/signatureOfSimpleInnerSimpleOuter.kt"); } + + @TestMetadata("suspendFunctionLiteralGenericSignature.kt") + public void testSuspendFunctionLiteralGenericSignature() throws Exception { + runTest("compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt"); + } } @TestMetadata("compiler/testData/codegen/box/reflection/isInstance") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 2ed960f898b..461d8c39137 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -22924,6 +22924,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testSignatureOfSimpleInnerSimpleOuter() throws Exception { runTest("compiler/testData/codegen/box/reflection/genericSignature/signatureOfSimpleInnerSimpleOuter.kt"); } + + @TestMetadata("suspendFunctionLiteralGenericSignature.kt") + public void testSuspendFunctionLiteralGenericSignature() throws Exception { + runTest("compiler/testData/codegen/box/reflection/genericSignature/suspendFunctionLiteralGenericSignature.kt"); + } } @TestMetadata("compiler/testData/codegen/box/reflection/isInstance")