From 98b0c07b18675b27c67411dade8e5fb76fa4e02a Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 26 Jan 2021 15:30:41 +0300 Subject: [PATCH] JVM_IR indy-SAM conversions: inline funs and lambdas KT-44278 KT-26060 KT-42621 --- .../FirBlackBoxCodegenTestGenerated.java | 22 ++++++++++++++++ .../jvm/lower/FunctionReferenceLowering.kt | 5 ++++ .../inlineFunInDifferentPackage.kt | 20 +++++++++++++++ .../sam/inlineContext/inlineLambda1.kt | 25 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 22 ++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 22 ++++++++++++++++ .../LightAnalysisModeTestGenerated.java | 23 +++++++++++++++++ .../IrJsCodegenBoxES6TestGenerated.java | 13 ++++++++++ .../IrJsCodegenBoxTestGenerated.java | 13 ++++++++++ .../semantics/JsCodegenBoxTestGenerated.java | 13 ++++++++++ .../IrCodegenBoxWasmTestGenerated.java | 13 ++++++++++ 11 files changed, 191 insertions(+) create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt create mode 100644 compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index c498716f469..18a24e94a93 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -18568,6 +18568,28 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature/genericFunInterfaceWithInlineString.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + public class InlineContext extends AbstractFirBlackBoxCodegenTest { + @Test + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); + } + + @Test + @TestMetadata("inlineFunInDifferentPackage.kt") + public void testInlineFunInDifferentPackage() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt"); + } + + @Test + @TestMetadata("inlineLambda1.kt") + public void testInlineLambda1() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.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 72c541077a1..4654ddf6dc5 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 @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.FileLoweringPass import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext import org.jetbrains.kotlin.backend.common.ir.* import org.jetbrains.kotlin.backend.common.lower.SamEqualsHashCodeMethodsGenerator +import org.jetbrains.kotlin.backend.common.lower.parents import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin @@ -135,6 +136,10 @@ internal class FunctionReferenceLowering(private val context: JvmBackendContext) ) return false + // Can't use indy-based SAM conversion inside inline fun (Ok in inline lambda). + if (target.parents.any { it is IrSimpleFunction && it.isInline && it.origin != IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA }) + return false + return true } diff --git a/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt b/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt new file mode 100644 index 00000000000..df4a44b7b64 --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt @@ -0,0 +1,20 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +// FILE: inlineFunInDifferentPackage.kt +import a.* + +fun box() = test { k -> "O" + k } + +// FILE: a.kt +package a + +fun interface IFoo { + fun foo(k: String): String +} + +fun fooK(iFoo: IFoo) = iFoo.foo("K") + +inline fun test(crossinline lambda: (String) -> String) = + fooK { k -> lambda(k) } \ No newline at end of file diff --git a/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt b/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt new file mode 100644 index 00000000000..47d7f8411ea --- /dev/null +++ b/compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt @@ -0,0 +1,25 @@ +// TARGET_BACKEND: JVM +// JVM_TARGET: 1.8 +// SAM_CONVERSIONS: INDY +// WITH_RUNTIME +fun interface IFoo { + fun foo() +} + +fun foo(iFoo: IFoo) = iFoo.foo() + +inline fun twice(fn: () -> Unit) { + fn() + fn() +} + +fun box(): String { + var test = 0 + twice { + foo { test += 1 } + } + if (test != 2) + return "Failed: test=$test" + + return "OK" +} \ 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 f34dd817d01..a1281d8134a 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 @@ -18568,6 +18568,28 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature/genericFunInterfaceWithInlineString.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + public class InlineContext extends AbstractBlackBoxCodegenTest { + @Test + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); + } + + @Test + @TestMetadata("inlineFunInDifferentPackage.kt") + public void testInlineFunInDifferentPackage() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt"); + } + + @Test + @TestMetadata("inlineLambda1.kt") + public void testInlineLambda1() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt"); + } + } } } 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 40c583ba90d..2c8fb657361 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 @@ -18568,6 +18568,28 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature/genericFunInterfaceWithInlineString.kt"); } } + + @Nested + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + public class InlineContext extends AbstractIrBlackBoxCodegenTest { + @Test + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); + } + + @Test + @TestMetadata("inlineFunInDifferentPackage.kt") + public void testInlineFunInDifferentPackage() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt"); + } + + @Test + @TestMetadata("inlineLambda1.kt") + public void testInlineLambda1() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt"); + } + } } } diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d609c82ae46..9354811bff6 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16280,6 +16280,29 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature/genericFunInterfaceWithInlineString.kt"); } } + + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class InlineContext extends AbstractLightAnalysisModeTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); + } + + @TestMetadata("inlineFunInDifferentPackage.kt") + public void testInlineFunInDifferentPackage() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineFunInDifferentPackage.kt"); + } + + @TestMetadata("inlineLambda1.kt") + public void testInlineLambda1() throws Exception { + runTest("compiler/testData/codegen/box/invokedynamic/sam/inlineContext/inlineLambda1.kt"); + } + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java index b20b0f768a4..81156d68bfe 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/es6/semantics/IrJsCodegenBoxES6TestGenerated.java @@ -13915,6 +13915,19 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true); } } + + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class InlineContext extends AbstractIrJsCodegenBoxES6Test { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR_ES6, testDataFilePath); + } + + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true); + } + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java index cdcef275234..949f18a22be 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/semantics/IrJsCodegenBoxTestGenerated.java @@ -13915,6 +13915,19 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); } } + + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class InlineContext extends AbstractIrJsCodegenBoxTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS_IR, testDataFilePath); + } + + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true); + } + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 486ca13b279..af9a372d26b 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -13980,6 +13980,19 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); } } + + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class InlineContext extends AbstractJsCodegenBoxTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest0(this::doTest, TargetBackend.JS, testDataFilePath); + } + + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true); + } + } } } diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java index a978a8889d1..d464975739d 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/wasm/semantics/IrCodegenBoxWasmTestGenerated.java @@ -8076,6 +8076,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineClassInSignature"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); } } + + @TestMetadata("compiler/testData/codegen/box/invokedynamic/sam/inlineContext") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class InlineContext extends AbstractIrCodegenBoxWasmTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath); + } + + public void testAllFilesPresentInInlineContext() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/invokedynamic/sam/inlineContext"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true); + } + } } }