|
| 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 | +} |
0 commit comments