Correctly support nullability in type arguments for serializer<T>() intrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2265
This commit is contained in:
Leonid Startsev
2023-04-19 12:03:32 +02:00
committed by Space Team
parent 8cc9f911d5
commit ef9074e24d
5 changed files with 64 additions and 3 deletions
@@ -225,13 +225,13 @@ class SerializationJvmIrIntrinsicSupport(
*
* Operation detection in new compilers performed by voidMagicApiCall.
*/
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: KotlinTypeMarker, intrinsicType: IntrinsicType): Boolean =
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: IrType, intrinsicType: IntrinsicType): Boolean =
with(typeSystemContext) {
val typeDescriptor = type.typeConstructor().getTypeParameterClassifier()
if (typeDescriptor != null) { // need further reification
ReifiedTypeInliner.putReifiedOperationMarkerIfNeeded(
typeDescriptor,
false,
type.isMarkedNullable(),
ReifiedTypeInliner.OperationKind.TYPE_OF,
this@putReifyMarkerIfNeeded,
typeSystemContext
@@ -0,0 +1,49 @@
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.modules.*
import java.lang.AssertionError
inline fun <reified T: Any?> listOfNullable(): KSerializer<List<T?>> = serializer<List<T?>>()
inline fun <reified T: Any> listOfNullableWithNonNullBound(): KSerializer<List<T?>> = serializer<List<T?>>()
inline fun <reified T> listOfNullableNoExplicitBound(): KSerializer<List<T?>> = serializer<List<T?>>()
inline fun <reified T> listOfNullableWithCast(): KSerializer<List<Any?>> = serializer<List<T?>>() as KSerializer<List<Any?>>
inline fun <reified T> listOfUnspecifiedNullability(): KSerializer<List<T>> = serializer<List<T>>()
inline fun <reified T> getSer(module: SerializersModule): KSerializer<T> {
return module.serializer<T>()
}
fun check(shouldBeNullable: Boolean, descriptor: SerialDescriptor) {
if (shouldBeNullable == descriptor.isNullable) return
if (shouldBeNullable) throw java.lang.AssertionError("Should be nullable, but is not: $descriptor")
throw java.lang.AssertionError("Should not be nullable, but it is: $descriptor")
}
fun box(): String {
check(false, serializer<String>().descriptor)
check(true, serializer<String?>().descriptor)
check(false, serializer<List<String>>().descriptor.elementDescriptors.first())
check(true, serializer<List<String?>>().descriptor.elementDescriptors.first())
check(true, serializer<List<String>?>().descriptor)
check(true, listOfNullable<String>().descriptor.elementDescriptors.first())
check(true, listOfNullableNoExplicitBound<String>().descriptor.elementDescriptors.first())
check(true, listOfNullableWithNonNullBound<String>().descriptor.elementDescriptors.first())
check(true, listOfNullableWithCast<String>().descriptor.elementDescriptors.first())
check(false, listOfUnspecifiedNullability<String>().descriptor.elementDescriptors.first())
check(true, listOfUnspecifiedNullability<String?>().descriptor.elementDescriptors.first())
val module = EmptySerializersModule()
check(false, getSer<String>(module).descriptor)
check(true, getSer<String?>(module).descriptor)
return "OK"
}
@@ -47,4 +47,4 @@ fun box(): String {
getListSer<Box<*>>()
}
return "OK"
}
}
@@ -135,6 +135,12 @@ public class SerializationFirLightTreeBlackBoxTestGenerated extends AbstractSeri
runTest("plugins/kotlinx-serialization/testData/boxIr/intrinsicsBox.kt");
}
@Test
@TestMetadata("intrinsicsNullable.kt")
public void testIntrinsicsNullable() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/intrinsicsNullable.kt");
}
@Test
@TestMetadata("intrinsicsStarProjections.kt")
public void testIntrinsicsStarProjections() throws Exception {
@@ -133,6 +133,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT
runTest("plugins/kotlinx-serialization/testData/boxIr/intrinsicsBox.kt");
}
@Test
@TestMetadata("intrinsicsNullable.kt")
public void testIntrinsicsNullable() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/intrinsicsNullable.kt");
}
@Test
@TestMetadata("intrinsicsStarProjections.kt")
public void testIntrinsicsStarProjections() throws Exception {