From 0daab88f97850d80c845df6a469ff09a67f23de9 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 15 Nov 2019 00:41:45 +0100 Subject: [PATCH] JVM IR: make RemoveInlinedDeclarations a final module phase Code in inline lambdas can call multifile part members. These calls are replaced in GenerateMultifileFacades with the call to the facade member. Previously this didn't happen though because the lambda body was removed before the GenerateMultifileFacades phase, which led to IllegalAccessError in the -Xmultifile-parts-inherit mode (because the part class is package private in another package). --- .../jetbrains/kotlin/backend/jvm/JvmLower.kt | 4 ++-- ...eDeclarationsThatWouldBeInlinedLowering.kt | 4 ++-- .../optimized/callInInlineLambda.kt | 19 +++++++++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 5 +++++ .../LightAnalysisModeTestGenerated.java | 5 +++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 5 +++++ 6 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt index b33f8c3be3a..28ee3d65dbf 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmLower.kt @@ -304,8 +304,6 @@ private val jvmFilePhases = mainMethodGenerationPhase then - // should be last transformation - removeDeclarationsThatWouldBeInlined then makePatchParentsPhase(3) val jvmPhases = namedIrModulePhase( @@ -316,6 +314,8 @@ val jvmPhases = namedIrModulePhase( fileClassPhase then performByIrFile(lower = jvmFilePhases) then generateMultifileFacadesPhase then + // should be last transformation + removeDeclarationsThatWouldBeInlined then validateIrAfterLowering ) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/RemoveDeclarationsThatWouldBeInlinedLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/RemoveDeclarationsThatWouldBeInlinedLowering.kt index a1b6b300860..b4f1dc0cce8 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/RemoveDeclarationsThatWouldBeInlinedLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/RemoveDeclarationsThatWouldBeInlinedLowering.kt @@ -6,7 +6,7 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.common.FileLoweringPass -import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase +import org.jetbrains.kotlin.backend.common.phaser.makeIrModulePhase import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.ir.isLambda import org.jetbrains.kotlin.ir.IrElement @@ -17,7 +17,7 @@ import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.expressions.IrFunctionReference import org.jetbrains.kotlin.ir.visitors.* -internal val removeDeclarationsThatWouldBeInlined = makeIrFilePhase( +internal val removeDeclarationsThatWouldBeInlined = makeIrModulePhase( ::RemoveDeclarationsThatWouldBeInlinedLowering, name = "RemoveInlinedDeclarations", description = "Rename declaration that should be inlined" diff --git a/compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt b/compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt new file mode 100644 index 00000000000..6eaacfc3209 --- /dev/null +++ b/compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt @@ -0,0 +1,19 @@ +// TARGET_BACKEND: JVM +// IGNORE_LIGHT_ANALYSIS +// WITH_RUNTIME +// !INHERIT_MULTIFILE_PARTS +// FILE: result.kt + +@file:JvmName("Util") +@file:JvmMultifileClass +package test + +fun result(): String = "OK" + +// FILE: test.kt + +import test.result + +private inline fun id(f: () -> String): String = f() + +fun box(): String = id { result() } diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index c3aab349ab1..3ee69fe82bf 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -16497,6 +16497,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/multifileClasses/optimized"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("callInInlineLambda.kt") + public void testCallInInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt"); + } + @TestMetadata("callableRefToConstVal.kt") public void testCallableRefToConstVal() throws Exception { runTest("compiler/testData/codegen/box/multifileClasses/optimized/callableRefToConstVal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 27f89252ed0..c7d1a6a6621 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -16497,6 +16497,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/multifileClasses/optimized"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("callInInlineLambda.kt") + public void testCallInInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt"); + } + @TestMetadata("callableRefToConstVal.kt") public void testCallableRefToConstVal() throws Exception { runTest("compiler/testData/codegen/box/multifileClasses/optimized/callableRefToConstVal.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index e6cf5de2f1f..77ff25dd2e6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -15372,6 +15372,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/multifileClasses/optimized"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); } + @TestMetadata("callInInlineLambda.kt") + public void testCallInInlineLambda() throws Exception { + runTest("compiler/testData/codegen/box/multifileClasses/optimized/callInInlineLambda.kt"); + } + @TestMetadata("callableRefToConstVal.kt") public void testCallableRefToConstVal() throws Exception { runTest("compiler/testData/codegen/box/multifileClasses/optimized/callableRefToConstVal.kt");