From ce7af9ff2b31c8390a1660ad614352dcf8c85263 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Wed, 8 Mar 2023 18:01:01 +0100 Subject: [PATCH] [K2, MPP] Support arguments with expect types in actual functions --- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 +++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 +++ .../common/actualizer/IrActualizerUtils.kt | 38 +++++++++++-------- .../actualFunctionWithArgumentOfExpectType.kt | 24 ++++++++++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++ .../IrBlackBoxCodegenTestGenerated.java | 6 +++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 +++ .../LightAnalysisModeTestGenerated.java | 5 +++ 8 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 6eb149fd517..b3926a12aed 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -33256,6 +33256,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @Test + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + @Test public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index 1802b5fd83a..0cd924504c1 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -33256,6 +33256,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @Test + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + @Test public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrActualizerUtils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrActualizerUtils.kt index 53fbbabe657..f0c40b3c72a 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrActualizerUtils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/actualizer/IrActualizerUtils.kt @@ -35,10 +35,24 @@ fun Map>.getMatch( } private fun IrFunction.match(actualFunction: IrFunction, expectActualTypesMap: Map): Boolean { + fun getActualizedValueParameterSymbol( + expectParameter: IrValueParameter, + localTypeParametersMap: Map? = null + ): IrSymbol { + return expectParameter.type.classifierOrFail.let { + val localMappedSymbol = if (localTypeParametersMap != null && it is IrTypeParameterSymbol) { + localTypeParametersMap[it] + } else { + null + } + localMappedSymbol ?: expectActualTypesMap[it] ?: it + } + } + fun checkParameter( expectParameter: IrValueParameter?, actualParameter: IrValueParameter?, - typeParametersMap: Map + localTypeParametersMap: Map ): Boolean { if (expectParameter == null) { return actualParameter == null @@ -47,17 +61,9 @@ private fun IrFunction.match(actualFunction: IrFunction, expectActualTypesMap: M return false } - val actualizedParameterTypeSymbol = expectParameter.type.classifierOrFail.let { - var mappedSymbol: IrSymbol? = null - if (it is IrTypeParameterSymbol) { - mappedSymbol = typeParametersMap[it] - } - if (mappedSymbol == null) { - mappedSymbol = expectActualTypesMap[it] ?: it - } - mappedSymbol - } - if (actualizedParameterTypeSymbol != actualParameter.type.classifierOrFail) { + if (getActualizedValueParameterSymbol(expectParameter, localTypeParametersMap) != + getActualizedValueParameterSymbol(actualParameter) + ) { return false } return true @@ -67,20 +73,20 @@ private fun IrFunction.match(actualFunction: IrFunction, expectActualTypesMap: M return false } - val typeParametersMap = mutableMapOf() + val localTypeParametersMap = mutableMapOf() for ((expectTypeParameter, actualTypeParameter) in typeParameters.zip(actualFunction.typeParameters)) { if (expectTypeParameter.name != actualTypeParameter.name) { return false } - typeParametersMap[expectTypeParameter.symbol] = actualTypeParameter.symbol + localTypeParametersMap[expectTypeParameter.symbol] = actualTypeParameter.symbol } - if (!checkParameter(extensionReceiverParameter, actualFunction.extensionReceiverParameter, typeParametersMap)) { + if (!checkParameter(extensionReceiverParameter, actualFunction.extensionReceiverParameter, localTypeParametersMap)) { return false } for ((expectParameter, actualParameter) in valueParameters.zip(actualFunction.valueParameters)) { - if (!checkParameter(expectParameter, actualParameter, typeParametersMap)) { + if (!checkParameter(expectParameter, actualParameter, localTypeParametersMap)) { return false } } diff --git a/compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt b/compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt new file mode 100644 index 00000000000..8274cb6aec9 --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt @@ -0,0 +1,24 @@ +// TARGET_BACKEND: JVM +// !LANGUAGE: +MultiPlatformProjects + +// MODULE: common0 +// TARGET_PLATFORM: Common +// FILE: common0.kt + +expect fun f0(s: S): S + +expect class S + +// MODULE: common1()()(common0) +// TARGET_PLATFORM: Common +// FILE: common1.kt + +actual fun f0(s: S): S = s + +// MODULE: jvm()()(common1) +// TARGET_PLATFORM: JVM +// FILE: jvm.kt + +actual typealias S = String + +fun box() = f0("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 766f9423a07..17585463d39 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 @@ -31888,6 +31888,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @Test + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + @Test public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); 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 a9782405bdc..89fa92985da 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 @@ -33256,6 +33256,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @Test + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + @Test public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java index 11d991f1b74..0ec1d5ece04 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java @@ -33256,6 +33256,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @Test + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + @Test public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true); diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index d927930c8b6..886174882ec 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -27168,6 +27168,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/multiplatform/k2/basic/accessToLocalClassFromBackend.kt"); } + @TestMetadata("actualFunctionWithArgumentOfExpectType.kt") + public void testActualFunctionWithArgumentOfExpectType() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/basic/actualFunctionWithArgumentOfExpectType.kt"); + } + public void testAllFilesPresentInBasic() throws Exception { KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/multiplatform/k2/basic"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true); }