diff --git a/core/commonMain/src/kotlinx/serialization/internal/BuiltInSerializers.kt b/core/commonMain/src/kotlinx/serialization/internal/BuiltInSerializers.kt index 544de3a0dd..2e64a770fd 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/BuiltInSerializers.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/BuiltInSerializers.kt @@ -27,7 +27,7 @@ internal object DurationSerializer : KSerializer { @PublishedApi internal object NothingSerializer : KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("kotlin.Nothing", PrimitiveKind.INT) + override val descriptor: SerialDescriptor = NothingSerialDescriptor override fun serialize(encoder: Encoder, value: Nothing) { throw SerializationException("'kotlin.Nothing' cannot be serialized") diff --git a/core/commonMain/src/kotlinx/serialization/internal/NothingSerialDescriptor.kt b/core/commonMain/src/kotlinx/serialization/internal/NothingSerialDescriptor.kt new file mode 100644 index 0000000000..aab492a018 --- /dev/null +++ b/core/commonMain/src/kotlinx/serialization/internal/NothingSerialDescriptor.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +@file:OptIn(ExperimentalSerializationApi::class) + +package kotlinx.serialization.internal + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.descriptors.SerialKind +import kotlinx.serialization.descriptors.StructureKind + +internal object NothingSerialDescriptor : SerialDescriptor { + public override val kind: SerialKind = StructureKind.OBJECT + + public override val serialName: String = "kotlin.Nothing" + + override val elementsCount: Int get() = 0 + override fun getElementName(index: Int): String = error() + override fun getElementIndex(name: String): Int = error() + override fun isElementOptional(index: Int): Boolean = error() + override fun getElementDescriptor(index: Int): SerialDescriptor = error() + override fun getElementAnnotations(index: Int): List = error() + override fun toString(): String = "NothingSerialDescriptor" + override fun equals(other: Any?): Boolean { + return this === other + } + + override fun hashCode(): Int = serialName.hashCode() + 31 * kind.hashCode() + private fun error(): Nothing = + throw IllegalStateException("Descriptor for type `kotlin.Nothing` does not have elements") +}