diff --git a/core/commonMain/src/kotlinx/serialization/Serializers.kt b/core/commonMain/src/kotlinx/serialization/Serializers.kt index 1892f6a7cd..2489be276f 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? { 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 c16945c942..ef313ccd97 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 +// 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> by given KSerializer, KSerializer, ... * via reflection (on JVM) or compiler+plugin intrinsic `SerializerFactory` (on Native)