From 9be5421e7c58f52165b03ce13d80f78ce12c5e5d Mon Sep 17 00:00:00 2001 From: Leonid Startsev Date: Thu, 18 Feb 2021 18:12:51 +0300 Subject: [PATCH] Use .nullable extension instead of NullableSerializer() constructor Use correct projection in createClassReference() so primitive-boxes arrays behave correctly on JVM --- .../compiler/backend/ir/GeneratorHelpers.kt | 42 +++++++++---------- .../compiler/resolve/NamingConventions.kt | 4 +- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt index 063706d30ef..26f224ea283 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/GeneratorHelpers.kt @@ -633,7 +633,7 @@ interface IrBuilderExtension { endOffset, compilerContext.irBuiltIns.kClassClass.starProjectedType, classSymbol, - classSymbol.starProjectedType + classType.toIrType() // todo: maybe this is jvm-specific behavior ) } @@ -713,8 +713,7 @@ interface IrBuilderExtension { dispatchReceiverParameter: IrValueParameter, property: SerializableProperty ): IrExpression? { - val nullableSerializerFqn = getInternalPackageFqn(SpecialBuiltins.nullableSerializer) - val nullableSerClass = compilerContext.referenceClass(nullableSerializerFqn) ?: error("Couldn't find class $nullableSerializerFqn") + val nullableSerClass = compilerContext.referenceProperties(SerialEntityNames.wrapIntoNullableExt).single() val serializer = property.serializableWith?.toClassDescriptor ?: if (!property.type.isTypeParameter()) generator.findTypeSerializerOrContext( @@ -736,24 +735,22 @@ interface IrBuilderExtension { private fun IrBuilderWithScope.wrapWithNullableSerializerIfNeeded( type: KotlinType, expression: IrExpression, - nullableSerializerClass: IrClassSymbol - ): IrExpression { - return if (type.isMarkedNullable) { - val classDeclaration = nullableSerializerClass.owner - val nullableConstructor = classDeclaration.declarations.first { it is IrConstructor } as IrConstructor - val resultType = type.makeNotNullable() - val typeParameters = classDeclaration.typeParameters - val typeArguments = listOf(resultType.toIrType()) - irInvoke( - null, nullableConstructor.symbol, - typeArguments = typeArguments, - valueArguments = listOf(expression), - // Return type should be correctly substituted - returnTypeHint = nullableConstructor.returnType.substitute(typeParameters, typeArguments) - ) - } else { - expression - } + nullableProp: IrPropertySymbol + ): IrExpression = if (type.isMarkedNullable) { + val resultType = type.makeNotNullable() + val typeArguments = listOf(resultType.toIrType()) + val callee = nullableProp.owner.getter!! + + val returnType = callee.returnType.substitute(callee.typeParameters, typeArguments) + + irInvoke( + callee = callee.symbol, + typeArguments = typeArguments, + valueArguments = emptyList(), + returnTypeHint = returnType + ).apply { extensionReceiver = expression } + } else { + expression } @@ -793,8 +790,7 @@ interface IrBuilderExtension { genericIndex: Int? = null, genericGetter: ((Int, KotlinType) -> IrExpression)? = null ): IrExpression? { - val nullableClassFqn = getInternalPackageFqn(SpecialBuiltins.nullableSerializer) - val nullableSerClass = compilerContext.referenceClass(nullableClassFqn) ?: error("Expecting class $nullableClassFqn") + val nullableSerClass = compilerContext.referenceProperties(SerialEntityNames.wrapIntoNullableExt).single() if (serializerClassOriginal == null) { if (genericIndex == null) return null return genericGetter?.invoke(genericIndex, kType) diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt index 2ad719718fc..b6466b202a3 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/resolve/NamingConventions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -95,6 +95,8 @@ object SerialEntityNames { // parameters val dummyParamName = Name.identifier("serializationConstructorMarker") internal const val typeArgPrefix = "typeSerial" + + internal val wrapIntoNullableExt = SerializationPackages.builtinsPackageFqName.child(Name.identifier("nullable")) } object SpecialBuiltins {