Skip to content

Commit ef9074e

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
1 parent 8cc9f91 commit ef9074e

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
@@ -225,13 +225,13 @@ class SerializationJvmIrIntrinsicSupport(
225225
*
226226
* Operation detection in new compilers performed by voidMagicApiCall.
227227
*/
228-
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: KotlinTypeMarker, intrinsicType: IntrinsicType): Boolean =
228+
private fun InstructionAdapter.putReifyMarkerIfNeeded(type: IrType, intrinsicType: IntrinsicType): Boolean =
229229
with(typeSystemContext) {
230230
val typeDescriptor = type.typeConstructor().getTypeParameterClassifier()
231231
if (typeDescriptor != null) { // need further reification
232232
ReifiedTypeInliner.putReifiedOperationMarkerIfNeeded(
233233
typeDescriptor,
234-
false,
234+
type.isMarkedNullable(),
235235
ReifiedTypeInliner.OperationKind.TYPE_OF,
236236
this@putReifyMarkerIfNeeded,
237237
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/SerializationFirLightTreeBlackBoxTestGenerated.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)