Skip to content

Commit ebdbaab

Browse files
sandwwraithSpace Team
authored and
Space Team
committed
Correctly support nullability in type arguments for serializer<T>() intrinsic.
Nullability info should be added to TYPE_OF operation marker. Fixes Kotlin/kotlinx.serialization#2265 (cherry picked from commit ef9074e)
1 parent 34efee5 commit ebdbaab

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,13 @@ class SerializationJvmIrIntrinsicSupport(val jvmBackendContext: JvmBackendContex
231231
*
232232
* Operation detection in new compilers performed by voidMagicApiCall.
233233
*/
234-
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: KotlinTypeMarker, intrinsicType: IntrinsicType): Boolean =
234+
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: IrType, intrinsicType: IntrinsicType): Boolean =
235235
with(typeSystemContext) {
236236
val typeDescriptor = type.typeConstructor().getTypeParameterClassifier()
237237
if (typeDescriptor != null) { // need further reification
238238
ReifiedTypeInliner.putReifiedOperationMarkerIfNeeded(
239239
typeDescriptor,
240-
false,
240+
type.isMarkedNullable(),
241241
ReifiedTypeInliner.OperationKind.TYPE_OF,
242242
this@putReifyMarkerIfNeeded,
243243
typeSystemContext
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// TARGET_BACKEND: JVM_IR
2+
3+
// WITH_STDLIB
4+
5+
import kotlinx.serialization.*
6+
import kotlinx.serialization.json.*
7+
import kotlinx.serialization.internal.*
8+
import kotlinx.serialization.descriptors.*
9+
import kotlinx.serialization.modules.*
10+
import java.lang.AssertionError
11+
12+
inline fun <reified T: Any?> listOfNullable(): KSerializer<List<T?>> = serializer<List<T?>>()
13+
inline fun <reified T: Any> listOfNullableWithNonNullBound(): KSerializer<List<T?>> = serializer<List<T?>>()
14+
inline fun <reified T> listOfNullableNoExplicitBound(): KSerializer<List<T?>> = serializer<List<T?>>()
15+
inline fun <reified T> listOfNullableWithCast(): KSerializer<List<Any?>> = serializer<List<T?>>() as KSerializer<List<Any?>>
16+
inline fun <reified T> listOfUnspecifiedNullability(): KSerializer<List<T>> = serializer<List<T>>()
17+
18+
inline fun <reified T> getSer(module: SerializersModule): KSerializer<T> {
19+
return module.serializer<T>()
20+
}
21+
22+
fun check(shouldBeNullable: Boolean, descriptor: SerialDescriptor) {
23+
if (shouldBeNullable == descriptor.isNullable) return
24+
if (shouldBeNullable) throw java.lang.AssertionError("Should be nullable, but is not: $descriptor")
25+
throw java.lang.AssertionError("Should not be nullable, but it is: $descriptor")
26+
}
27+
28+
fun box(): String {
29+
check(false, serializer<String>().descriptor)
30+
check(true, serializer<String?>().descriptor)
31+
32+
check(false, serializer<List<String>>().descriptor.elementDescriptors.first())
33+
check(true, serializer<List<String?>>().descriptor.elementDescriptors.first())
34+
check(true, serializer<List<String>?>().descriptor)
35+
36+
check(true, listOfNullable<String>().descriptor.elementDescriptors.first())
37+
check(true, listOfNullableNoExplicitBound<String>().descriptor.elementDescriptors.first())
38+
check(true, listOfNullableWithNonNullBound<String>().descriptor.elementDescriptors.first())
39+
check(true, listOfNullableWithCast<String>().descriptor.elementDescriptors.first())
40+
41+
check(false, listOfUnspecifiedNullability<String>().descriptor.elementDescriptors.first())
42+
check(true, listOfUnspecifiedNullability<String?>().descriptor.elementDescriptors.first())
43+
44+
val module = EmptySerializersModule()
45+
check(false, getSer<String>(module).descriptor)
46+
check(true, getSer<String?>(module).descriptor)
47+
48+
return "OK"
49+
}

plugins/kotlinx-serialization/testData/boxIr/intrinsicsStarProjections.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ fun box(): String {
4747
getListSer<Box<*>>()
4848
}
4949
return "OK"
50-
}
50+
}

plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationFirBlackBoxTestGenerated.java

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/kotlinx-serialization/tests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)