From a75408b1ed52c109acf33588ebb6599f42383d61 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 17 Mar 2020 18:25:02 +0100 Subject: [PATCH] JVM_IR: use non-nullable types as SAM super types The wrappers for conversions to nullable and non-nullable SAM types are the same, so differentiating those has no effect other than class name collisions. --- .../ir/FirBlackBoxCodegenTestGenerated.java | 5 +++++ .../lower/SingleAbstractMethodLowering.kt | 4 ++-- .../codegen/box/sam/nonInlinedSamWrapper.kt | 22 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/sam/nonInlinedSamWrapper.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 6405188cce1..6344551cec8 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 @@ -25741,6 +25741,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nonInlinedSamWrapper.kt") + public void testNonInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); + } + @TestMetadata("nullableSam.kt") public void testNullableSam() throws Exception { runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt index a562880c418..0de1f340441 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/SingleAbstractMethodLowering.kt @@ -104,8 +104,8 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) : val inInlineFunctionScope = allScopes.any { scope -> (scope.irElement as? IrFunction)?.isInline ?: false } val cache = if (inInlineFunctionScope) inlineCachedImplementations else cachedImplementations - val implementation = cache.getOrPut(superType) { - createObjectProxy(superType, inInlineFunctionScope, expression) + val implementation = cache.getOrPut(erasedSuperType) { + createObjectProxy(erasedSuperType, inInlineFunctionScope, expression) } return if (superType.isNullable() && invokable.type.isNullable()) { diff --git a/compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt b/compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt new file mode 100644 index 00000000000..38384311ff3 --- /dev/null +++ b/compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt @@ -0,0 +1,22 @@ +// IGNORE_BACKEND_FIR: JVM_IR +// TARGET_BACKEND: JVM +// WITH_RUNTIME +// FILE: Test.java + +public class Test { + public static void run(Runnable r) { + r.run(); + } +} + +// FILE: test.kt + +fun runFunction1(x: () -> Unit) = Runnable(x).run() +fun runFunction2(x: () -> Unit) = Test.run(x) + +fun box(): String { + var result = "" + runFunction1 { result += "O" } + runFunction2 { result += "K" } + return result +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index a5eb82fc38b..62f516a33e9 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -27302,6 +27302,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nonInlinedSamWrapper.kt") + public void testNonInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); + } + @TestMetadata("nullableSam.kt") public void testNullableSam() throws Exception { runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 4aa4531974a..e8771bb6648 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -26119,6 +26119,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nonInlinedSamWrapper.kt") + public void testNonInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); + } + @TestMetadata("nullableSam.kt") public void testNullableSam() throws Exception { runTest("compiler/testData/codegen/box/sam/nullableSam.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 51915589172..5ed7f74d03e 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -25741,6 +25741,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/sam/kt24825.kt"); } + @TestMetadata("nonInlinedSamWrapper.kt") + public void testNonInlinedSamWrapper() throws Exception { + runTest("compiler/testData/codegen/box/sam/nonInlinedSamWrapper.kt"); + } + @TestMetadata("nullableSam.kt") public void testNullableSam() throws Exception { runTest("compiler/testData/codegen/box/sam/nullableSam.kt");