Skip to content

Commit

Permalink
Slightly improve error messages thrown from serializer<T>() function (#…
Browse files Browse the repository at this point in the history
…2533)

and mention intrinsics to keep them in sync.
  • Loading branch information
sandwwraith authored Jan 8, 2024
1 parent cd9f8b0 commit c10428e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions core/commonMain/src/kotlinx/serialization/Serializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c10428e

Please sign in to comment.