From 20a5c44e41f8cf4d02ae8ae2409f4487b9f70202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Thu, 24 Sep 2020 16:49:19 +0200 Subject: [PATCH] JVM IR: Fix types in generated function reference constructor For references to suspend functions this fixes an IllegalStateException in the type mapper, which is what the "suspendFunctionMethodReference" test is about. --- .../codegen/ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../backend/jvm/lower/FunctionReferenceLowering.kt | 8 ++------ .../box/coroutines/suspendFunctionMethodReference.kt | 11 +++++++++++ .../kotlin/codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../codegen/LightAnalysisModeTestGenerated.java | 5 +++++ .../codegen/ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ .../es6/semantics/IrJsCodegenBoxES6TestGenerated.java | 5 +++++ .../ir/semantics/IrJsCodegenBoxTestGenerated.java | 5 +++++ .../js/test/semantics/JsCodegenBoxTestGenerated.java | 5 +++++ 9 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt diff --git a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index cd956d283d6..7b065747c7a 100644 --- a/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -6903,6 +6903,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendImplBridge.kt") public void testSuspendImplBridge() throws Exception { runTest("compiler/testData/codegen/box/coroutines/suspendImplBridge.kt"); diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt index 2e9918feb91..1236644d2a9 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/FunctionReferenceLowering.kt @@ -273,12 +273,8 @@ internal class FunctionReferenceLowering(private val context: JvmBackendContext) }.apply { // Add receiver parameter for bound function references if (samSuperType == null) { - boundReceiver?.first?.let { param -> - valueParameters += param.copyTo( - irFunction = this, - index = 0, - type = param.type.substitute(typeArgumentsMap) - ) + boundReceiver?.let { (param, arg) -> + valueParameters += param.copyTo(irFunction = this, index = 0, type = arg.type) } } diff --git a/compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt b/compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt new file mode 100644 index 00000000000..41f588731e6 --- /dev/null +++ b/compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt @@ -0,0 +1,11 @@ +// IGNORE_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR + +fun f(a: suspend () -> Unit): String { + val f = a::invoke + return "OK" +} + +fun box(): String { + return f {} +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index cba207c3b9a..e45326de720 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -7303,6 +7303,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendImplBridge.kt") public void testSuspendImplBridge() throws Exception { runTest("compiler/testData/codegen/box/coroutines/suspendImplBridge.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a586ed8ef99..d62e4368b2b 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -6466,6 +6466,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) public static class Coroutines extends AbstractLightAnalysisModeTest { + @TestMetadata("suspendFunctionMethodReference.kt") + public void ignoreSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + private void runTest(String testDataFilePath) throws Exception { KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 326a99bcf77..9ac4bde3f2e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -6903,6 +6903,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendImplBridge.kt") public void testSuspendImplBridge() throws Exception { runTest("compiler/testData/codegen/box/coroutines/suspendImplBridge.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index 1cc0c2d3d78..b7597e93fd5 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -5753,6 +5753,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendInCycle.kt", "kotlin.coroutines"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index 8a7857d4b5a..2dc6cef1fb2 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -5753,6 +5753,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendInCycle.kt", "kotlin.coroutines"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index bc4be412bae..a9b83274f1f 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -5753,6 +5753,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendFunImportedFromObject.kt", "kotlin.coroutines"); } + @TestMetadata("suspendFunctionMethodReference.kt") + public void testSuspendFunctionMethodReference() throws Exception { + runTest("compiler/testData/codegen/box/coroutines/suspendFunctionMethodReference.kt"); + } + @TestMetadata("suspendInCycle.kt") public void testSuspendInCycle_1_3() throws Exception { runTestWithPackageReplacement("compiler/testData/codegen/box/coroutines/suspendInCycle.kt", "kotlin.coroutines");