Do not create cacheableChildSerializers unless it is necessary

Creation of this property in the SerializerIrGenerator.<init> can lead to
a 'Serializer not found' internal error when generator is applied to a fully-customized external serializer.
In that case, generator is still created, but none of the generateSave/Load functions are called,
so cacheableChildSerializers(Property) is not necessary.

#KT-57730 Fixed
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2260
This commit is contained in:
Leonid Startsev
2023-04-05 18:35:18 +02:00
committed by Space Team
parent 54c07f5ebb
commit d9e16fb76e
5 changed files with 58 additions and 10 deletions
@@ -66,11 +66,9 @@ open class SerializerIrGenerator(
SerialEntityNames.SERIAL_DESC_FIELD
) { true }?.takeIf { it.isFromPlugin(compilerContext.afterK2) }
protected val anySerialDescProperty = getProperty(
protected val irAnySerialDescProperty = getProperty(
SerialEntityNames.SERIAL_DESC_FIELD,
) { true } // remove true?
protected val irAnySerialDescProperty = anySerialDescProperty
) { true }
fun getProperty(
name: String,
@@ -83,13 +81,16 @@ open class SerializerIrGenerator(
private set
// child serializers cached if serializable class is internal
private val cachedChildSerializersProperty = if (isGeneratedSerializer)
serializableIrClass.companionObject()?.properties?.singleOrNull { it.name == CACHED_CHILD_SERIALIZERS_PROPERTY_NAME }
else null
private val cachedChildSerializersProperty by lazy {
if (isGeneratedSerializer)
serializableIrClass.companionObject()?.properties?.singleOrNull { it.name == CACHED_CHILD_SERIALIZERS_PROPERTY_NAME }
else null
}
// non-object serializers which can be cached
private val cacheableChildSerializers =
private val cacheableChildSerializers by lazy {
serializableIrClass.createCachedChildSerializers(properties.serializableProperties).map { it != null }
}
// null if was not found — we're in FIR
private fun findLocalSerializersFieldDescriptors(): List<IrProperty?> {
@@ -230,7 +231,12 @@ open class SerializerIrGenerator(
val allSerializers = serializableProperties.mapIndexed { index, property ->
requireNotNull(
serializerTower(this@SerializerIrGenerator, irFun.dispatchReceiverParameter!!, property, cachedChildSerializerByIndex(index))
serializerTower(
this@SerializerIrGenerator,
irFun.dispatchReceiverParameter!!,
property,
cachedChildSerializerByIndex(index)
)
) { "Property ${property.name} must have a serializer" }
}