Skip to content

Commit c10428e

Browse files
authored
Slightly improve error messages thrown from serializer<T>() function (#2533)
and mention intrinsics to keep them in sync.
1 parent cd9f8b0 commit c10428e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

core/commonMain/src/kotlinx/serialization/Serializers.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ private fun SerializersModule.serializerByKTypeImpl(
187187
): KSerializer<Any?>? {
188188
val rootClass = type.kclass()
189189
val isNullable = type.isMarkedNullable
190-
val typeArguments = type.arguments
191-
.map { requireNotNull(it.type) { "Star projections in type arguments are not allowed, but had $type" } }
190+
val typeArguments = type.arguments.map(KTypeProjection::typeOrThrow)
192191

193192
val cachedSerializer = if (typeArguments.isEmpty()) {
194193
findCachedSerializer(rootClass, isNullable)

core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,22 @@ internal expect fun KClass<*>.platformSpecificSerializerNotRegistered(): Nothing
102102
internal fun KType.kclass() = when (val t = classifier) {
103103
is KClass<*> -> t
104104
is KTypeParameter -> {
105-
error(
105+
// If you are going to change this error message, please also actualize the message in the compiler intrinsics here:
106+
// Kotlin/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializationJvmIrIntrinsicSupport.kt#argumentTypeOrGenerateException
107+
throw IllegalArgumentException(
106108
"Captured type parameter $t from generic non-reified function. " +
107-
"Such functionality cannot be supported as $t is erased, either specify serializer explicitly or make " +
108-
"calling function inline with reified $t"
109+
"Such functionality cannot be supported because $t is erased, either specify serializer explicitly or make " +
110+
"calling function inline with reified $t."
109111
)
110112
}
111113

112-
else -> error("Only KClass supported as classifier, got $t")
114+
else -> throw IllegalArgumentException("Only KClass supported as classifier, got $t")
113115
} as KClass<Any>
114116

117+
// If you are going to change this error message, please also actualize the message in the compiler intrinsics here:
118+
// Kotlin/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializationJvmIrIntrinsicSupport.kt#argumentTypeOrGenerateException
119+
internal fun KTypeProjection.typeOrThrow(): KType = requireNotNull(type) { "Star projections in type arguments are not allowed, but had $type" }
120+
115121
/**
116122
* Constructs KSerializer<D<T0, T1, ...>> by given KSerializer<T0>, KSerializer<T1>, ...
117123
* via reflection (on JVM) or compiler+plugin intrinsic `SerializerFactory` (on Native)

0 commit comments

Comments
 (0)