From fb97f4db87916e292d992af519062160de7217a6 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 28 Mar 2023 14:10:05 +0300 Subject: [PATCH] [FIR] Don't create generated nested classifier scope for non-source classes ^KT-57626 Fixed --- .../impl/FirDeclaredMemberScopeProvider.kt | 7 ++++-- .../boxIr/serializableFromAnotherModule.kt | 25 +++++++++++++++++++ ...tionFirLightTreeBlackBoxTestGenerated.java | 6 +++++ .../SerializationIrBoxTestGenerated.java | 6 +++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDeclaredMemberScopeProvider.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDeclaredMemberScopeProvider.kt index 1aa7fdba9c3..7f4d6a17d90 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDeclaredMemberScopeProvider.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirDeclaredMemberScopeProvider.kt @@ -91,11 +91,14 @@ class FirDeclaredMemberScopeProvider(val useSiteSession: FirSession) : FirSessio } private fun createNestedClassifierScope(klass: FirClass): FirNestedClassifierScope? { - return if (klass.origin.generated) { + val origin = klass.origin + return if (origin.generated) { FirGeneratedClassNestedClassifierScope.create(useSiteSession, klass, baseScope = null) } else { val baseScope = FirNestedClassifierScopeImpl(klass, useSiteSession) - val generatedScope = FirGeneratedClassNestedClassifierScope.create(useSiteSession, klass, baseScope) + val generatedScope = runIf(origin.fromSource) { + FirGeneratedClassNestedClassifierScope.create(useSiteSession, klass, baseScope) + } if (generatedScope != null) { FirCompositeNestedClassifierScope( listOf(baseScope, generatedScope), diff --git a/plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt b/plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt new file mode 100644 index 00000000000..0702c235906 --- /dev/null +++ b/plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt @@ -0,0 +1,25 @@ +// TARGET_BACKEND: JVM_IR +// WITH_STDLIB +// ISSUE: KT-57626 + +// MODULE: lib +import kotlinx.serialization.* + +@Serializable +data class A(val s: String = "") + +// MODULE: main(lib) +import kotlinx.serialization.* +import kotlinx.serialization.json.* + +@Serializable +data class B(val a: A? = null) + +fun box(): String { + val expectedB = B(A("OK")) + val json = Json.encodeToString(B.serializer(), expectedB) + if (json != """{"a":{"s":"OK"}}""") return "Fail: $json" + val actualB = Json.decodeFromString(B.serializer(), json) + if (expectedB != actualB) return "Fail: $actualB" + return "OK" +} diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java index 824d3522dd5..0859f60b835 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java @@ -177,6 +177,12 @@ public class SerializationFirLightTreeBlackBoxTestGenerated extends AbstractSeri runTest("plugins/kotlinx-serialization/testData/boxIr/serialInfo.kt"); } + @Test + @TestMetadata("serializableFromAnotherModule.kt") + public void testSerializableFromAnotherModule() throws Exception { + runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt"); + } + @Test @TestMetadata("serializableOnPropertyType.kt") public void testSerializableOnPropertyType() throws Exception { diff --git a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java index 3cc1b56a8b4..5a6df348563 100644 --- a/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java +++ b/plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java @@ -175,6 +175,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT runTest("plugins/kotlinx-serialization/testData/boxIr/serialInfo.kt"); } + @Test + @TestMetadata("serializableFromAnotherModule.kt") + public void testSerializableFromAnotherModule() throws Exception { + runTest("plugins/kotlinx-serialization/testData/boxIr/serializableFromAnotherModule.kt"); + } + @Test @TestMetadata("serializableOnPropertyType.kt") public void testSerializableOnPropertyType() throws Exception {