From 886ce055f54f44addddd0f54bfa239012d4b35a0 Mon Sep 17 00:00:00 2001 From: Mads Ager Date: Mon, 31 May 2021 14:56:53 +0200 Subject: [PATCH] [JVM_IR] Do not use invoke-dynamic for targeting inline-only methods. Fixes https://youtrack.jetbrains.com/issue/KT-46962 --- .../FirBlackBoxCodegenTestGenerated.java | 6 ++++++ .../lower/indy/LambdaMetafactoryArguments.kt | 5 +++++ .../box/invokedynamic/sam/inlineOnly.kt | 21 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 ++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ 6 files changed, 49 insertions(+) create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.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 0650db96c25..547481f8033 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 @@ -21217,6 +21217,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/invokedynamic/sam/genericLambdaSignature.kt"); } + @Test + @TestMetadata("inlineOnly.kt") + public void testInlineOnly() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt"); + } + @Test @TestMetadata("insideInitBlock.kt") public void testInsideInitBlock() throws Exception { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt index 13f10745b5f..e4836483d74 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/indy/LambdaMetafactoryArguments.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.inline.INLINE_ONLY_ANNOTATION_FQ_NAME import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.utils.addIfNotNull @@ -82,6 +83,10 @@ internal class LambdaMetafactoryArgumentsBuilder( if (context.irIntrinsics.getIntrinsic(implFun.symbol) != null) return null + // Don't generate reference to inlineOnly methods as those do not exist at runtime. + if (implFun.hasAnnotation(INLINE_ONLY_ANNOTATION_FQ_NAME)) + return null + if (implFun is IrConstructor && implFun.visibility.isPrivate) { // Kotlin generates constructor accessors differently from Java. // TODO more precise accessibility check (see SyntheticAccessorLowering::isAccessible) diff --git a/compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt b/compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt new file mode 100644 index 00000000000..8f60c66cf4d --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt @@ -0,0 +1,21 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME + +// CHECK_BYTECODE_TEXT +// JVM_IR_TEMPLATES +// 0 java/lang/invoke/LambdaMetafactory + +fun interface Consumer { + fun foo(s: String) +} + +fun call(c: Consumer) { +} + +fun box(): String { + // println is inline only and therefore we cannot use invoke-dynamic to target it. + call(::println) + return "OK" +} 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 e2ac8f51110..f32b45c4e80 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 @@ -21187,6 +21187,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/invokedynamic/sam/genericLambdaSignature.kt"); } + @Test + @TestMetadata("inlineOnly.kt") + public void testInlineOnly() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt"); + } + @Test @TestMetadata("insideInitBlock.kt") public void testInsideInitBlock() 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 b5b2c8a1062..fd5b1a2989f 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 @@ -21217,6 +21217,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/invokedynamic/sam/genericLambdaSignature.kt"); } + @Test + @TestMetadata("inlineOnly.kt") + public void testInlineOnly() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt"); + } + @Test @TestMetadata("insideInitBlock.kt") public void testInsideInitBlock() 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 5844454bafd..8c72fa159b7 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -17706,6 +17706,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/invokedynamic/sam/genericFunInterfaceWithPrimitive.kt"); } + @TestMetadata("inlineOnly.kt") + public void testInlineOnly() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineOnly.kt"); + } + @TestMetadata("insideInitBlock.kt") public void testInsideInitBlock() throws Exception { runTest("compiler/testData/codegen/box/invokedynamic/sam/insideInitBlock.kt");