Make $serializer generic constructor public

If a serializable class has generic type parameters, its serializer is not an object
and has a specialized constructor. This constructor was public in K1 and should
be public in K2 so it can be called from other serializable classes
(in case class is e.g., part of sealed hierarchy).

#KT-63402 Fixed
This commit is contained in:
Leonid Startsev
2023-11-23 15:40:17 +01:00
committed by Space Team
parent 51e3e9af94
commit 662bff7351
6 changed files with 43 additions and 5 deletions
@@ -260,7 +260,7 @@ class SerializationFirResolveExtension(session: FirSession) : FirDeclarationGene
if (owner.name == SerialEntityNames.SERIALIZER_CLASS_NAME && owner.typeParameterSymbols.isNotEmpty()) {
result += createConstructor(owner, SerializationPluginKey) {
visibility = Visibilities.Private
visibility = Visibilities.Public
owner.typeParameterSymbols.forEachIndexed { i, typeParam ->
valueParameter(
name = Name.identifier("${SerialEntityNames.typeArgPrefix}$i"),
@@ -0,0 +1,28 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
// FILE: a.kt
import kotlinx.serialization.*
@Serializable
sealed class Base {
abstract val id: Long
}
// FILE: b.kt
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
@Serializable
class DerivedOtherFile<Value>(
override val id: Long,
val value: Value
) : Base()
fun box(): String {
val desc = Base.serializer().descriptor.kind
return if (desc == PolymorphicKind.SEALED) "OK" else desc.toString()
}
@@ -5,9 +5,7 @@ public final class Box$$serializer : java/lang/Object, kotlinx/serialization/int
private void <init>()
private void <init>(kotlinx.serialization.KSerializer typeSerial0)
public void <init>(kotlinx.serialization.KSerializer typeSerial0, kotlin.jvm.internal.DefaultConstructorMarker $constructor_marker)
public void <init>(kotlinx.serialization.KSerializer typeSerial0)
public final kotlinx.serialization.KSerializer[] childSerializers()
@@ -38,7 +38,7 @@ FILE: classWithGenericParameters.kt
super<R|kotlin/Any|>()
}
private constructor<T, V>(typeSerial0: R|kotlinx/serialization/KSerializer<T>|, typeSerial1: R|kotlinx/serialization/KSerializer<V>|): R|GenericBox.$serializer<T, V>|
public constructor<T, V>(typeSerial0: R|kotlinx/serialization/KSerializer<T>|, typeSerial1: R|kotlinx/serialization/KSerializer<V>|): R|GenericBox.$serializer<T, V>|
}
@@ -213,6 +213,12 @@ public class SerializationFirLightTreeBlackBoxTestGenerated extends AbstractSeri
runTest("plugins/kotlinx-serialization/testData/boxIr/repeatableSerialInfo.kt");
}
@Test
@TestMetadata("sealedClassMultifile.kt")
public void testSealedClassMultifile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedClassMultifile.kt");
}
@Test
@TestMetadata("sealedInterfaces.kt")
public void testSealedInterfaces() throws Exception {
@@ -211,6 +211,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT
runTest("plugins/kotlinx-serialization/testData/boxIr/repeatableSerialInfo.kt");
}
@Test
@TestMetadata("sealedClassMultifile.kt")
public void testSealedClassMultifile() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/sealedClassMultifile.kt");
}
@Test
@TestMetadata("sealedInterfaces.kt")
public void testSealedInterfaces() throws Exception {