Added support serialization of local classes in IR backend

Resolves Kotlin/kotlinx.serialization#1427
This commit is contained in:
Sergey Shanshin
2021-06-02 16:43:31 +00:00
committed by Space
parent ac0dc94800
commit 71c9e62d64
9 changed files with 143 additions and 22 deletions
@@ -1,17 +1,42 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE,-EXPERIMENTAL_API_USAGE_ERROR,-EXPERIMENTAL_API_USAGE
// WITH_RUNTIME
// SKIP_TXT
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
fun container() {
<!LOCAL_CLASSES_NOT_SUPPORTED!>@Serializable<!>
class X
@Serializable
class X // local classes are allowed
val y = <!LOCAL_CLASSES_NOT_SUPPORTED!>@Serializable<!> object {}
val y = <!ANONYMOUS_OBJECTS_NOT_SUPPORTED!>@Serializable<!> object {
fun inObjectFun() {
<!ANONYMOUS_OBJECTS_NOT_SUPPORTED!>@Serializable<!>
class X // local classes in anonymous object functions are not allowed
}
}
class LocalSerializer : KSerializer<Any?> {
override val descriptor: SerialDescriptor = buildSerialDescriptor("tmp", PrimitiveKind.INT)
override fun serialize(encoder: Encoder, value: Any?) {
encoder.encodeNull()
}
override fun deserialize(decoder: Decoder): Any? {
return decoder.decodeNull()
}
}
@Serializable
class WithLocalSerializerInProperty(<!LOCAL_SERIALIZER_USAGE!>@Serializable(with = LocalSerializer::class)<!> val x: Any?)
<!LOCAL_SERIALIZER_USAGE, SERIALIZER_TYPE_INCOMPATIBLE!>@Serializable(with = LocalSerializer::class)<!>
data class WithLocalSerializer(val i: Int)
}
val topLevelAnon = <!LOCAL_CLASSES_NOT_SUPPORTED!>@Serializable<!> object {}
val topLevelAnon = <!ANONYMOUS_OBJECTS_NOT_SUPPORTED!>@Serializable<!> object {}
@Serializable class A {
@Serializable class B // nested classes are allowed
@@ -19,4 +44,4 @@ val topLevelAnon = <!LOCAL_CLASSES_NOT_SUPPORTED!>@Serializable<!> object {}
<!INNER_CLASSES_NOT_SUPPORTED!>@Serializable<!> inner class C // inner classes are not
@Serializable object F {} // regular named object, OK
}
}
@@ -14,6 +14,9 @@ object BazSerializer: KSerializer<Baz>
@Serializer(forClass = Baz::class)
object NullableBazSerializer: KSerializer<Baz?>
<!SERIALIZER_TYPE_INCOMPATIBLE!>@Serializable(with = BazSerializer::class)<!>
class Biz(val i: Int)
@Serializable
class Foo(@Serializable(with = BazSerializer::class) val i: <!SERIALIZER_TYPE_INCOMPATIBLE!>Bar<!>)