From 003d07b51e7114cba448aecea9cb62e6f5de302c Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Wed, 31 Mar 2021 18:40:03 +0300 Subject: [PATCH] [K/N][IR] Ref to expect property in actual declaration is not remapped ^KT-36880 --- .../jetbrains/kotlin/backend/konan/ir/ModuleIndex.kt | 12 ++++++++++++ .../konan/lower/ExpectDeclarationsRemoving.kt | 11 ++++++++++- kotlin-native/backend.native/tests/build.gradle | 7 +++++++ .../codegen/mpp/remap_expect_property_ref_lib.kt | 11 +++++++++++ .../codegen/mpp/remap_expect_property_ref_main.kt | 1 + 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_lib.kt create mode 100644 kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_main.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/ModuleIndex.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/ModuleIndex.kt index b07d70a5412..5d5e45edcd1 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/ModuleIndex.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/ModuleIndex.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.backend.konan.ir import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid @@ -25,9 +26,15 @@ class ModuleIndex(val module: IrModuleFragment) { */ val functions: Map + /** + * Contains all properties declared in [module] + */ + val properties: Map + init { classes = mutableMapOf() functions = mutableMapOf() + properties = mutableMapOf() module.acceptVoid(object : IrElementVisitorVoid { override fun visitElement(element: IrElement) { @@ -44,6 +51,11 @@ class ModuleIndex(val module: IrModuleFragment) { super.visitFunction(declaration) functions[declaration.descriptor] = declaration } + + override fun visitProperty(declaration: IrProperty) { + super.visitProperty(declaration) + properties[declaration.descriptor] = declaration + } }) } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ExpectDeclarationsRemoving.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ExpectDeclarationsRemoving.kt index bb29eaa07d4..26aaac98218 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ExpectDeclarationsRemoving.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ExpectDeclarationsRemoving.kt @@ -89,9 +89,12 @@ internal class ExpectToActualDefaultValueCopier(private val irModule: IrModuleFr }) } - private inline fun T.findActualForExpected(): T = + private inline fun T.findActualForExpected(): T = moduleIndex.functions[descriptor.findActualForExpect()] as T + private fun IrProperty.findActualForExpected(): IrProperty = + moduleIndex.properties[descriptor.findActualForExpect()]!! + private fun IrClass.findActualForExpected(): IrClass = moduleIndex.classes[descriptor.findActualForExpect()]!! @@ -147,6 +150,12 @@ internal class ExpectToActualDefaultValueCopier(private val irModule: IrModuleFr else -> super.getReferencedSimpleFunction(symbol) } + override fun getReferencedProperty(symbol: IrPropertySymbol) = + if (symbol.descriptor.isExpect) + symbol.owner.findActualForExpected().symbol + else + super.getReferencedProperty(symbol) + override fun getReferencedValue(symbol: IrValueSymbol) = remapExpectValue(symbol)?.symbol ?: super.getReferencedValue(symbol) } diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 4759a786b62..198efc51ff1 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -4640,6 +4640,13 @@ linkTest("private_fake_overrides_1") { goldValue = "PASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\nPASS\n" } +linkTest("remap_expect_property_refs") { + source = "codegen/mpp/remap_expect_property_ref_main.kt" + lib = "codegen/mpp/remap_expect_property_ref_lib.kt" + flags = ["-Xmulti-platform"] + goldValue = "42\n" +} + Task frameworkTest(String name, Closure configurator) { return KotlinNativeTestKt.createTest(project, name, FrameworkTest) { task -> configurator.delegate = task diff --git a/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_lib.kt b/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_lib.kt new file mode 100644 index 00000000000..d46a10ca9a2 --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_lib.kt @@ -0,0 +1,11 @@ +expect class Foo { + val p: Int + fun bar(r: () -> Int = this::p): Int +} +actual class Foo { + actual val p = 42 + actual fun bar(r: () -> Int) = r() +} +fun main() { + println(Foo().bar()) +} diff --git a/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_main.kt b/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_main.kt new file mode 100644 index 00000000000..8b1a393741c --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/mpp/remap_expect_property_ref_main.kt @@ -0,0 +1 @@ +// empty