From 85c222633554031c44883aefca841739c719fbbf Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 13 Sep 2023 13:47:50 +0300 Subject: [PATCH] [FIR2IR] Allow symbolTable cache lookups for mapped JVM declarations --- .../fir/backend/Fir2IrDeclarationStorage.kt | 21 +++++++++-- ...LightTreeBlackBoxCodegenTestGenerated.java | 6 ++++ ...hIrFakeOverrideGeneratorTestGenerated.java | 6 ++++ .../FirPsiBlackBoxCodegenTestGenerated.java | 6 ++++ ...eclarationsUpdatedMembersInCommonModule.kt | 36 +++++++++++++++++++ .../IrBlackBoxCodegenTestGenerated.java | 6 ++++ ...kBoxCodegenWithIrInlinerTestGenerated.java | 6 ++++ .../LightAnalysisModeTestGenerated.java | 5 +++ 8 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 84a1c88db45..65ce45cb943 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.visibility import org.jetbrains.kotlin.fir.descriptors.FirBuiltInsPackageFragment import org.jetbrains.kotlin.fir.descriptors.FirModuleDescriptor import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression +import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyClass import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyConstructor import org.jetbrains.kotlin.fir.references.toResolvedValueParameterSymbol @@ -1025,7 +1026,6 @@ class Fir2IrDeclarationStorage( fakeOverrideOwnerLookupTag ?: declaration.containingClassLookupTag()!! ) irFakeOverridesForFirFakeOverrideMap[key] = cachedInSymbolTable - return cachedInSymbolTable } configuration.useIrFakeOverrideBuilder -> { /* @@ -1036,12 +1036,27 @@ class Fir2IrDeclarationStorage( * TODO: potentially this situation won't happen after migration from FIR2IR f/o generator to IR f/o generator (see KT-58861) */ cache[declaration] = cachedInSymbolTable - return cachedInSymbolTable + } + declaration.initialSignatureAttr != null -> { + /* + * FIR creates remapped functions for builtin JVM classes based on use-site session, not declaration site + * It leads to the situations when we have two different mapped FIR functions for the same original function + * (and same IR function) + */ + cache[declaration] = cachedInSymbolTable + } + declaration.symbol is FirJavaOverriddenSyntheticPropertySymbol -> { + /* + * Synthetic properties for java classes, if those properties are based on real Kotlin properties are also session + * dependant + */ + cache[declaration] = cachedInSymbolTable } else -> { - error("IR declaration with signature $signature found in SymbolTable and not found in declaration storage") + error("IR declaration with signature \"$signature\" found in SymbolTable and not found in declaration storage") } } + return cachedInSymbolTable } return null 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 5ea23975a61..356c25d8047 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 @@ -34044,6 +34044,12 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @Test + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java index 34af5e5a4f2..69719ffab72 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated.java @@ -34044,6 +34044,12 @@ public class FirLightTreeBlackBoxCodegenWithIrFakeOverrideGeneratorTestGenerated runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @Test + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") 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 1b42bb3d9c3..fc64ab27bdf 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 @@ -34044,6 +34044,12 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @Test + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt b/compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt new file mode 100644 index 00000000000..9a4e9c2a172 --- /dev/null +++ b/compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt @@ -0,0 +1,36 @@ +// TARGET_BACKEND: JVM_IR +// LANGUAGE: +MultiPlatformProjects +// WITH_STDLIB +// FULL_JDK + +// MODULE: common +// FILE: common.kt +/* + * During JVM compilation compiler creates mapped function for java.util.HashMap.contains twice (because they are session-dependant) + * The same happens with renamed synthetic properties based on real kotlin properties (LinkedHashMap.keySet() -> LinkedHashMap.keys) + */ +fun boxCommon(x: LinkedHashMap?): String { + val res = hashSetOf().contains(12) + x?.keys + return if (res) { + "Error" + } else { + "O" + } +} + +// MODULE: platform()()(common) +// FILE: platform.kt +fun boxPlatform(x: LinkedHashMap?): String { + val res = hashSetOf().contains(12) + x?.keys + return if (res) { + "Error" + } else { + "K" + } +} + +fun box(): String { + return boxCommon(null) + boxPlatform(null) +} 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 2981a26f156..0584fdcce5d 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 @@ -34044,6 +34044,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @Test + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") 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 b7260def428..70df3a998f5 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 @@ -34044,6 +34044,12 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @Test + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index a8f454eb06d..bf96be567c0 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -29011,6 +29011,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/multiplatform/k2/javaMethodWithTypeParameter.kt"); } + @TestMetadata("jvmDeclarationsUpdatedMembersInCommonModule.kt") + public void testJvmDeclarationsUpdatedMembersInCommonModule() throws Exception { + runTest("compiler/testData/codegen/box/multiplatform/k2/jvmDeclarationsUpdatedMembersInCommonModule.kt"); + } + @TestMetadata("compiler/testData/codegen/box/multiplatform/k2/annotations") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)