From 916379c0e76eb08cf06e84468e65d0923792432b Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 10 Nov 2021 16:03:34 +0300 Subject: [PATCH] JVM KT-49613 use adapter for indy reference to protected constructor --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++ .../backend/jvm/lower/TypeOperatorLowering.kt | 2 +- .../lower/indy/LambdaMetafactoryArguments.kt | 12 ++++++-- .../sam/functionRefToJavaInterface/kt49613.kt | 28 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 ++++ 7 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 803ec120e15..1281c853ce5 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -22666,6 +22666,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt46408.kt"); } + @Test + @TestMetadata("kt49613.kt") + public void testKt49613() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt"); + } + @Test @TestMetadata("localFunction1.kt") public void testLocalFunction1() throws Exception { diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt index 32f8a3ce774..c6ebf70f43f 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt @@ -553,7 +553,7 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil targetRef: IrFunctionReference ): IrCall { fun fail(message: String): Nothing = - throw AssertionError("$message, irFunRef:\n${targetRef.dump()}") + throw AssertionError("$message, targetRef:\n${targetRef.dump()}") val dynamicCallArguments = ArrayList() diff --git a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt index 46afbcee460..17b52e145c9 100644 --- a/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt +++ b/compiler/ir/backend.jvm/lower/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt @@ -145,9 +145,8 @@ internal class LambdaMetafactoryArgumentsBuilder( functionHazard = true } - if (implFun is IrConstructor && DescriptorVisibilities.isPrivate(implFun.visibility)) { + if (isConstructorRequiringAccessor(implFun)) { // Kotlin generates constructor accessors differently from Java. - // We don't do exact accessibility check here, just functionHazard = true } @@ -214,6 +213,15 @@ internal class LambdaMetafactoryArgumentsBuilder( ?: MetafactoryArgumentsResult.Failure.FunctionHazard } + private fun isConstructorRequiringAccessor(implFun: IrFunction): Boolean { + if (implFun !is IrConstructor) return false + // We don't do exact accessibility check here: + // constructor will be called by a class generated by LambdaMetafactory at runtime. + val visibility = implFun.visibility + return visibility == DescriptorVisibilities.PROTECTED || + DescriptorVisibilities.isPrivate(visibility) + } + private val javaIoSerializableFqn = FqName("java.io").child(Name.identifier("Serializable")) diff --git a/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt new file mode 100644 index 00000000000..7a207e0eb94 --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt @@ -0,0 +1,28 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// FILE: kt49613.kt + +interface GetStep { + fun get(): Any +} + +class Outer protected constructor(val ok: Any) { + constructor(): this("xxx") + + val obj = object : GetStep { + override fun get() = Step(::Outer) + } +} + +fun box(): String { + val s = Outer().obj.get() as Step + val t = s.step("OK") as Outer + return t.ok as String +} + +// FILE: Step.java + +public interface Step { + Object step(String string); +} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index b53f0078755..644e5149756 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -22504,6 +22504,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt46408.kt"); } + @Test + @TestMetadata("kt49613.kt") + public void testKt49613() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt"); + } + @Test @TestMetadata("localFunction1.kt") public void testLocalFunction1() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index aa0afa9d9aa..acd23135403 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -22666,6 +22666,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt46408.kt"); } + @Test + @TestMetadata("kt49613.kt") + public void testKt49613() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt"); + } + @Test @TestMetadata("localFunction1.kt") public void testLocalFunction1() throws Exception { diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 059a6e90102..d9d4305b1d2 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -18840,6 +18840,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt46408.kt"); } + @TestMetadata("kt49613.kt") + public void testKt49613() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/kt49613.kt"); + } + @TestMetadata("localFunction1.kt") public void testLocalFunction1() throws Exception { runTest("compiler/testData/codegen/box/invokedynamic/sam/functionRefToJavaInterface/localFunction1.kt");