Skip to content

Commit a9dcaeb

Browse files
sandwwraithSpace Team
authored andcommitted
[kx.serialization] Bring back legacy enum serializer instantiation
It should work unless we raise minimal supported runtime to 1.5.0
1 parent 9e1fcba commit a9dcaeb

File tree

1 file changed

+19
-12
lines changed
  • plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir

1 file changed

+19
-12
lines changed

plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/Instantiator.kt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,8 @@ internal class Instantiator(
3939
) {
4040
val nullableSerClass = compilerContext.referenceProperties(SerialEntityNames.wrapIntoNullableCallableId).single()
4141

42-
init {
43-
val hasNewCtxSerCtor = compilerContext.referenceConstructors(contextSerializerId)
44-
.any { it.owner.hasShape(regularParameters = 3) }
45-
46-
val hasEnumFactories =
47-
compilerContext.enumSerializerFactoryFunc != null && compilerContext.annotatedEnumSerializerFactoryFunc != null
48-
49-
assert(hasNewCtxSerCtor && hasEnumFactories) { "Your serialization runtime is pre-historical, bruh" }
50-
}
42+
val hasEnumFactories =
43+
compilerContext.enumSerializerFactoryFunc != null && compilerContext.annotatedEnumSerializerFactoryFunc != null
5144

5245
data class Args(
5346
val args: List<IrExpression>, val typeArgs: List<IrType>,
@@ -86,7 +79,7 @@ internal class Instantiator(
8679
typeArgs = listOf(kType),
8780
)
8881
sealedSerializerId -> return instantiateSealedSerializer(serializerClass, kType)
89-
enumSerializerId -> return instantiateEnumSerializer(kType)
82+
enumSerializerId -> return instantiateEnumSerializer(serializerClass, kType)
9083
referenceArraySerializerId -> {
9184
val (origArgs, origTypeArgs) = regularArgs(typeArgumentsAsTypes) ?: return null
9285
val args = listOf(generator.wrapperClassReference(typeArgumentsAsTypes.single())) + origArgs
@@ -165,7 +158,8 @@ internal class Instantiator(
165158
return if (!needToCopyAnnotations) {
166159
constructors.single { it.owner.isPrimary }
167160
} else {
168-
constructors.find { it.owner.lastArgumentIsAnnotationArray() } ?: error("Your serialization runtime is pre-historical, bruh")
161+
constructors.find { it.owner.lastArgumentIsAnnotationArray() }
162+
?: error("Your serialization runtime is lower than minimal supported version (1.3.0). Please update your runtime.")
169163
}
170164
}
171165

@@ -210,7 +204,7 @@ internal class Instantiator(
210204
return Args(args, typeArgs)
211205
}
212206

213-
context(irBuilder: IrBuilderWithScope) private fun instantiateEnumSerializer(kType: IrSimpleType): IrExpression {
207+
context(irBuilder: IrBuilderWithScope) private fun instantiateEnumSerializer(serializerClass: IrClassSymbol, kType: IrSimpleType): IrExpression {
214208
val enumDescriptor = kType.classOrNull!!
215209
val typeArgs = listOf(kType)
216210
// instantiate serializer only inside enum Companion
@@ -224,6 +218,8 @@ internal class Instantiator(
224218
irBuilder.irCall(enumDescriptor.owner.findEnumValuesMethod()),
225219
)
226220

221+
if (!hasEnumFactories) return instantiateLegacyEnumSerializer(serializerClass, enumArgs, typeArgs)
222+
227223
// runtime contains enum serializer factory functions
228224
val factoryFunc: IrSimpleFunctionSymbol = if (enumDescriptor.owner.isEnumWithSerialInfoAnnotation()) {
229225
// need to store SerialInfo annotation in descriptor
@@ -260,6 +256,17 @@ internal class Instantiator(
260256
return generator.irInvoke(factoryFunc, enumArgs, typeArgs, factoryReturnType)
261257
}
262258

259+
context(irBuilder: IrBuilderWithScope)
260+
private fun instantiateLegacyEnumSerializer(
261+
serializerClass: IrClassSymbol,
262+
enumArgs: List<IrExpression>,
263+
typeArgs: List<IrSimpleType>,
264+
): IrExpression {
265+
assert(serializerClass.owner.classId == enumSerializerId) { "Expected enum serializer, got $serializerClass" }
266+
val ctor = findConstructorWithoutTypeParameters(serializerClass, needToCopyAnnotations = false).owner
267+
return callConstructor(ctor, typeArgs, enumArgs)
268+
}
269+
263270
context(irBuilder: IrBuilderWithScope)
264271
private fun regularArgs(typeArgumentsAsTypes: List<IrType>): Args? {
265272
val args = typeArgumentsAsTypes.map {

0 commit comments

Comments
 (0)