From b82c306530ccb3df842d8ed7c519ed8c768b2b55 Mon Sep 17 00:00:00 2001 From: Alexander Korepanov Date: Thu, 9 Dec 2021 14:12:00 +0300 Subject: [PATCH] [JS IR] Add deep copying for callable reference Perform a deep copy of callable reference adapter on inline lowering ^KT-49844 Fixed --- ...FirBlackBoxInlineCodegenTestGenerated.java | 6 +++ .../common/lower/inline/FunctionInlining.kt | 53 ++++++++++++------- .../multipleCallableReferenceUsage.kt | 34 ++++++++++++ .../BlackBoxInlineCodegenTestGenerated.java | 6 +++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 +++ .../IrBlackBoxInlineCodegenTestGenerated.java | 6 +++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 +++ ...otlinAgainstInlineKotlinTestGenerated.java | 6 +++ ...JvmIrAgainstOldBoxInlineTestGenerated.java | 6 +++ ...JvmOldAgainstIrBoxInlineTestGenerated.java | 6 +++ .../js/test/JsCodegenInlineTestGenerated.java | 6 +++ .../ir/IrJsCodegenInlineTestGenerated.java | 6 +++ .../blackboxtest/ExternalTestGenerated.java | 6 +++ 13 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxInlineCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxInlineCodegenTestGenerated.java index 0b922a8a0e3..6915db22833 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxInlineCodegenTestGenerated.java @@ -1116,6 +1116,12 @@ public class FirBlackBoxInlineCodegenTestGenerated extends AbstractFirBlackBoxIn public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt index 7dcf15ae297..7ebda1338e0 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/inline/FunctionInlining.kt @@ -245,10 +245,17 @@ class FunctionInlining( return super.visitCall(expression) return when { - functionArgument is IrFunctionReference -> inlineFunctionReference(expression, functionArgument) - functionArgument.isAdaptedFunctionReference() -> inlineAdaptedFunctionReference(expression, functionArgument as IrBlock) - functionArgument is IrFunctionExpression -> inlineFunctionExpression(expression, functionArgument) - else -> super.visitCall(expression) + functionArgument is IrFunctionReference -> + inlineFunctionReference(expression, functionArgument, functionArgument.symbol.owner) + + functionArgument.isAdaptedFunctionReference() -> + inlineAdaptedFunctionReference(expression, functionArgument as IrBlock) + + functionArgument is IrFunctionExpression -> + inlineFunctionExpression(expression, functionArgument) + + else -> + super.visitCall(expression) } } @@ -264,10 +271,12 @@ class FunctionInlining( } fun inlineAdaptedFunctionReference(irCall: IrCall, irBlock: IrBlock): IrExpression { - val irFunction = irBlock.statements[0] as IrFunction - irFunction.transformChildrenVoid(this) + val irFunction = irBlock.statements[0].let { + it.transformChildrenVoid(this) + copyIrElement.copy(it) as IrFunction + } val irFunctionReference = irBlock.statements[1] as IrFunctionReference - val inlinedFunctionReference = inlineFunctionReference(irCall, irFunctionReference) + val inlinedFunctionReference = inlineFunctionReference(irCall, irFunctionReference, irFunction) return IrBlockImpl( irCall.startOffset, irCall.endOffset, inlinedFunctionReference.type, origin = null, @@ -275,7 +284,11 @@ class FunctionInlining( ) } - fun inlineFunctionReference(irCall: IrCall, irFunctionReference: IrFunctionReference): IrExpression { + fun inlineFunctionReference( + irCall: IrCall, + irFunctionReference: IrFunctionReference, + inlinedFunction: IrFunction + ): IrExpression { irFunctionReference.transformChildrenVoid(this) val function = irFunctionReference.symbol.owner @@ -294,14 +307,14 @@ class FunctionInlining( } val immediateCall = with(irCall) { - when (function) { + when (inlinedFunction) { is IrConstructor -> { - val classTypeParametersCount = function.parentAsClass.typeParameters.size + val classTypeParametersCount = inlinedFunction.parentAsClass.typeParameters.size IrConstructorCallImpl.fromSymbolOwner( startOffset, endOffset, - function.returnType, - function.symbol, + inlinedFunction.returnType, + inlinedFunction.symbol, classTypeParametersCount ) } @@ -309,13 +322,13 @@ class FunctionInlining( IrCallImpl( startOffset, endOffset, - function.returnType, - function.symbol, - function.typeParameters.size, - function.valueParameters.size + inlinedFunction.returnType, + inlinedFunction.symbol, + inlinedFunction.typeParameters.size, + inlinedFunction.valueParameters.size ) else -> - error("Unknown function kind : ${function.render()}") + error("Unknown function kind : ${inlinedFunction.render()}") } }.apply { for (parameter in functionParameters) { @@ -353,15 +366,15 @@ class FunctionInlining( } when (parameter) { function.dispatchReceiverParameter -> - this.dispatchReceiver = argument.implicitCastIfNeededTo(function.dispatchReceiverParameter!!.type) + this.dispatchReceiver = argument.implicitCastIfNeededTo(inlinedFunction.dispatchReceiverParameter!!.type) function.extensionReceiverParameter -> - this.extensionReceiver = argument.implicitCastIfNeededTo(function.extensionReceiverParameter!!.type) + this.extensionReceiver = argument.implicitCastIfNeededTo(inlinedFunction.extensionReceiverParameter!!.type) else -> putValueArgument( parameter.index, - argument.implicitCastIfNeededTo(function.valueParameters[parameter.index].type) + argument.implicitCastIfNeededTo(inlinedFunction.valueParameters[parameter.index].type) ) } } diff --git a/compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt b/compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt new file mode 100644 index 00000000000..d96a16e567f --- /dev/null +++ b/compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt @@ -0,0 +1,34 @@ +// WITH_STDLIB +// KJS_WITH_FULL_RUNTIME + +// FILE: 1.kt +package test + +inline fun doIt(f: () -> Int): Int = f() +inline fun calcOnePlusTwo(f: (Int) -> Int): Int = f(1) + f(2) +inline fun getFirstArg(a: Int, vararg other: Int): Int = a + +fun testCustomFunction(): Boolean { + val x = doIt { calcOnePlusTwo(::getFirstArg) } + return x == 3 +} + +fun testRuntimeFunctionCase1(): Boolean { + val x = "123".let { it.minOf(::maxOf) } + return x == '1' +} + +fun testRuntimeFunctionCase2(): Boolean { + val x = "3123".minOfOrNull { a: Char -> a.titlecase().maxOf(::maxOf) } + return x == '1' +} + +// FILE: 2.kt +import test.* + +fun box(): String { + if (!testCustomFunction()) return "testCustomFunction failed" + if (!testRuntimeFunctionCase1()) return "testRuntimeFunctionCase1 failed" + if (!testRuntimeFunctionCase2()) return "testRuntimeFunctionCase2 failed" + return "OK" +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxInlineCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxInlineCodegenTestGenerated.java index 1bcf7f5feaf..3da66dcd972 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxInlineCodegenTestGenerated.java @@ -1116,6 +1116,12 @@ public class BlackBoxInlineCodegenTestGenerated extends AbstractBlackBoxInlineCo public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java index fec08646f54..9969503938f 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/CompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1116,6 +1116,12 @@ public class CompileKotlinAgainstInlineKotlinTestGenerated extends AbstractCompi public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxInlineCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxInlineCodegenTestGenerated.java index 277f62a2bb6..0597f082adf 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxInlineCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxInlineCodegenTestGenerated.java @@ -1116,6 +1116,12 @@ public class IrBlackBoxInlineCodegenTestGenerated extends AbstractIrBlackBoxInli public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrCompileKotlinAgainstInlineKotlinTestGenerated.java index de42794e8ac..d81d6b15413 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1116,6 +1116,12 @@ public class IrCompileKotlinAgainstInlineKotlinTestGenerated extends AbstractIrC public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated.java index e1f0e97d275..9a19fb162d0 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated.java @@ -1116,6 +1116,12 @@ public class IrSerializeCompileKotlinAgainstInlineKotlinTestGenerated extends Ab public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxInlineTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxInlineTestGenerated.java index 62c8197eea5..32fe0bdb6fa 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxInlineTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmIrAgainstOldBoxInlineTestGenerated.java @@ -1116,6 +1116,12 @@ public class JvmIrAgainstOldBoxInlineTestGenerated extends AbstractJvmIrAgainstO public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxInlineTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxInlineTestGenerated.java index d314d478025..a8a60582497 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxInlineTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/JvmOldAgainstIrBoxInlineTestGenerated.java @@ -1116,6 +1116,12 @@ public class JvmOldAgainstIrBoxInlineTestGenerated extends AbstractJvmOldAgainst public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenInlineTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenInlineTestGenerated.java index cc8cf46c4cb..9974cdf6212 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenInlineTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/JsCodegenInlineTestGenerated.java @@ -918,6 +918,12 @@ public class JsCodegenInlineTestGenerated extends AbstractJsCodegenInlineTest { public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenInlineTestGenerated.java b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenInlineTestGenerated.java index 268b0b55d38..f6fb1fb5088 100644 --- a/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenInlineTestGenerated.java +++ b/js/js.tests/tests-gen/org/jetbrains/kotlin/js/test/ir/IrJsCodegenInlineTestGenerated.java @@ -918,6 +918,12 @@ public class IrJsCodegenInlineTestGenerated extends AbstractIrJsCodegenInlineTes public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/ExternalTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/ExternalTestGenerated.java index da24ab9958a..f5e91a48da8 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/ExternalTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/blackboxtest/ExternalTestGenerated.java @@ -36326,6 +36326,12 @@ public class ExternalTestGenerated extends AbstractExternalNativeBlackBoxTest { public void testInlineVarargInts() throws Exception { runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/inlineVarargInts.kt"); } + + @Test + @TestMetadata("multipleCallableReferenceUsage.kt") + public void testMultipleCallableReferenceUsage() throws Exception { + runTest("compiler/testData/codegen/boxInline/callableReference/adaptedReferences/multipleCallableReferenceUsage.kt"); + } } @Nested