Skip to content

Commit

Permalink
Correctly support nullability in type arguments for serializer<T>() i…
Browse files Browse the repository at this point in the history
…ntrinsic.

Nullability info should be added to TYPE_OF operation marker.

Fixes Kotlin/kotlinx.serialization#2265

(cherry picked from commit ef9074e)
  • Loading branch information
sandwwraith authored and qodana-bot committed Apr 19, 2023
1 parent 34efee5 commit ebdbaab
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ class SerializationJvmIrIntrinsicSupport(val jvmBackendContext: JvmBackendContex
*
* 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
Expand Down
49 changes: 49 additions & 0 deletions plugins/kotlinx-serialization/testData/boxIr/intrinsicsNullable.kt
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ fun box(): String {
getListSer<Box<*>>()
}
return "OK"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebdbaab

Please sign in to comment.