diff options
author | Leonid Startsev <sandwwraith@users.noreply.github.com> | 2024-01-08 14:08:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-08 14:08:56 +0100 |
commit | c10428e5d1fc03cbb012db5c8be0c98aafe8dd16 (patch) | |
tree | 4055a454b1c1565e3dfdd6cfe172af006da09b5e | |
parent | cd9f8b0094c5e88ae30fc7baba3478e83aef8ebd (diff) | |
download | kotlinx.serialization-c10428e5d1fc03cbb012db5c8be0c98aafe8dd16.tar.gz |
Slightly improve error messages thrown from serializer<T>() function (#2533)
and mention intrinsics to keep them in sync.
-rw-r--r-- | core/commonMain/src/kotlinx/serialization/Serializers.kt | 3 | ||||
-rw-r--r-- | core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt | 14 |
2 files changed, 11 insertions, 6 deletions
diff --git a/core/commonMain/src/kotlinx/serialization/Serializers.kt b/core/commonMain/src/kotlinx/serialization/Serializers.kt index 1892f6a7..2489be27 100644 --- a/core/commonMain/src/kotlinx/serialization/Serializers.kt +++ b/core/commonMain/src/kotlinx/serialization/Serializers.kt @@ -187,8 +187,7 @@ private fun SerializersModule.serializerByKTypeImpl( ): KSerializer<Any?>? { val rootClass = type.kclass() val isNullable = type.isMarkedNullable - val typeArguments = type.arguments - .map { requireNotNull(it.type) { "Star projections in type arguments are not allowed, but had $type" } } + val typeArguments = type.arguments.map(KTypeProjection::typeOrThrow) val cachedSerializer = if (typeArguments.isEmpty()) { findCachedSerializer(rootClass, isNullable) diff --git a/core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt b/core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt index c16945c9..ef313ccd 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/Platform.common.kt @@ -102,16 +102,22 @@ internal expect fun KClass<*>.platformSpecificSerializerNotRegistered(): Nothing internal fun KType.kclass() = when (val t = classifier) { is KClass<*> -> t is KTypeParameter -> { - error( + // If you are going to change this error message, please also actualize the message in the compiler intrinsics here: + // Kotlin/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializationJvmIrIntrinsicSupport.kt#argumentTypeOrGenerateException + throw IllegalArgumentException( "Captured type parameter $t from generic non-reified function. " + - "Such functionality cannot be supported as $t is erased, either specify serializer explicitly or make " + - "calling function inline with reified $t" + "Such functionality cannot be supported because $t is erased, either specify serializer explicitly or make " + + "calling function inline with reified $t." ) } - else -> error("Only KClass supported as classifier, got $t") + else -> throw IllegalArgumentException("Only KClass supported as classifier, got $t") } as KClass<Any> +// If you are going to change this error message, please also actualize the message in the compiler intrinsics here: +// Kotlin/plugins/kotlinx-serialization/kotlinx-serialization.backend/src/org/jetbrains/kotlinx/serialization/compiler/backend/ir/SerializationJvmIrIntrinsicSupport.kt#argumentTypeOrGenerateException +internal fun KTypeProjection.typeOrThrow(): KType = requireNotNull(type) { "Star projections in type arguments are not allowed, but had $type" } + /** * Constructs KSerializer<D<T0, T1, ...>> by given KSerializer<T0>, KSerializer<T1>, ... * via reflection (on JVM) or compiler+plugin intrinsic `SerializerFactory` (on Native) |