Skip to content

Commit

Permalink
Fix Kotlin user projects build (#2341)
Browse files Browse the repository at this point in the history
* Remove @SharedImmutable usages

The annotation is deprecated and has no effect.

* Suppress animal sniffer error in a data class with boolean property

The error is a result of changes in data class hashCode generation.
The generated code now uses the static Boolean.hashCode() to calculate hash code of boolean properties. The static function is available only since Java 1.8.
  • Loading branch information
qurbonzoda committed Jun 23, 2023
1 parent 39918cb commit d192d24
Show file tree
Hide file tree
Showing 22 changed files with 17 additions and 30 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -204,6 +204,8 @@ subprojects {
case "kotlinx-serialization-hocon":
annotationValue = "kotlinx.serialization.hocon.internal.SuppressAnimalSniffer"
break
case "kotlinx-serialization-protobuf":
annotationValue = "kotlinx.serialization.protobuf.internal.SuppressAnimalSniffer"
}
annotation = annotationValue
}
Expand Down
Expand Up @@ -65,7 +65,6 @@ internal fun SerialDescriptor.cachedSerialNames(): Set<String> {
return result
}

@SharedImmutable
private val EMPTY_DESCRIPTOR_ARRAY: Array<SerialDescriptor> = arrayOf()

/**
Expand Down
Expand Up @@ -8,7 +8,6 @@ import kotlinx.serialization.*
import kotlin.jvm.*
import kotlin.native.concurrent.*

@SharedImmutable
@JvmField
internal val EMPTY_SERIALIZER_ARRAY: Array<KSerializer<*>> = arrayOf()

Expand Down
Expand Up @@ -16,7 +16,6 @@ import kotlin.reflect.*
import kotlin.time.Duration

@OptIn(ExperimentalUnsignedTypes::class)
@SharedImmutable
private val BUILTIN_SERIALIZERS = mapOf(
String::class to String.serializer(),
Char::class to Char.serializer(),
Expand Down
Expand Up @@ -11,7 +11,6 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlin.native.concurrent.*

@SharedImmutable
private val NULL = Any()
private const val deprecationMessage =
"This class is used only by the plugin in generated code and should not be used directly. Use corresponding factory functions instead"
Expand Down
Expand Up @@ -73,7 +73,6 @@ public sealed class SerializersModule {
/**
* A [SerializersModule] which is empty and always returns `null`.
*/
@SharedImmutable
@Deprecated("Deprecated in the favour of 'EmptySerializersModule()'",
level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith("EmptySerializersModule()"))
Expand Down
Expand Up @@ -30,7 +30,6 @@ open class PolyBase(val id: Int) {
@Serializable
data class PolyDerived(val s: String) : PolyBase(1)

@SharedImmutable
val BaseAndDerivedModule = SerializersModule {
polymorphic(PolyBase::class, PolyBase.serializer()) {
subclass(PolyDerived.serializer())
Expand Down
1 change: 0 additions & 1 deletion core/commonTest/src/kotlinx/serialization/UmbrellaTypes.kt
Expand Up @@ -67,7 +67,6 @@ data class ArraysUmbrella(
arrIntData.contentEquals(other.arrIntData)
}

@SharedImmutable
val umbrellaInstance = TypesUmbrella(
Unit, true, 10, 20, 30, 40, 50.1f, 60.1, 'A', "Str0", Attitude.POSITIVE, IntData(70),
null, null, 11, 21, 31, 41, 51.1f, 61.1, 'B', "Str1", Attitude.NEUTRAL, null,
Expand Down
Expand Up @@ -5,7 +5,5 @@
package kotlinx.serialization.test

import kotlinx.serialization.test.Platform
import kotlin.native.concurrent.SharedImmutable

@SharedImmutable
public actual val currentPlatform: Platform = Platform.NATIVE
Expand Up @@ -19,7 +19,6 @@ data class SimpleStringInheritor(val s: String, val i: Int) : SimpleAbstract()
@Serializable
data class PolyBox(@Polymorphic val boxed: SimpleAbstract)

@SharedImmutable
val SimplePolymorphicModule = SerializersModule {
polymorphic(SimpleAbstract::class) {
subclass(SimpleIntInheritor.serializer())
Expand Down
Expand Up @@ -37,7 +37,6 @@ class PolyDefaultWithId(id: Int) : PolyBase(id)
@Serializable
data class PolyDerived(val s: String) : PolyBase(1)

@SharedImmutable
val BaseAndDerivedModule = SerializersModule {
polymorphic(PolyBase::class, PolyBase.serializer()) {
subclass(PolyDerived.serializer())
Expand Down
Expand Up @@ -64,7 +64,6 @@ data class ArraysUmbrella(
arrIntData.contentEquals(other.arrIntData)
}

@SharedImmutable
val umbrellaInstance = TypesUmbrella(
Unit, true, 10, 20, 30, 40, 50.1f, 60.1, 'A', "Str0", Attitude.POSITIVE, IntData(70),
null, null, 11, 21, 31, 41, 51.1f, 61.1, 'B', "Str1", Attitude.NEUTRAL, null,
Expand Down
Expand Up @@ -38,7 +38,6 @@ internal data class OuterBox(@Polymorphic val outerBase: OuterBase, @Polymorphic
@Serializable
internal data class OuterNullableBox(@Polymorphic val outerBase: OuterBase?, @Polymorphic val innerBase: InnerBase?)

@SharedImmutable
internal val polymorphicTestModule = SerializersModule {
polymorphic(InnerBase::class) {
subclass(InnerImpl.serializer())
Expand All @@ -51,13 +50,11 @@ internal val polymorphicTestModule = SerializersModule {
}
}

@SharedImmutable
internal val polymorphicJson = Json {
serializersModule = polymorphicTestModule
encodeDefaults = true
}

@SharedImmutable
internal val polymorphicRelaxedJson = Json {
isLenient = true
serializersModule = polymorphicTestModule
Expand Down
Expand Up @@ -4,8 +4,5 @@

package kotlinx.serialization.test

import kotlin.native.concurrent.SharedImmutable


@SharedImmutable
public actual val currentPlatform: Platform = Platform.NATIVE
Expand Up @@ -11,7 +11,6 @@ import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.internal.InlinePrimitiveDescriptor
import kotlinx.serialization.json.internal.*
import kotlin.native.concurrent.SharedImmutable

/**
* Class representing single JSON element.
Expand Down Expand Up @@ -134,7 +133,6 @@ public fun JsonUnquotedLiteral(value: String?): JsonPrimitive {
}

/** Used as a marker to indicate during encoding that the [JsonEncoder] should use `encodeInline()` */
@SharedImmutable
internal val jsonUnquotedLiteralDescriptor: SerialDescriptor =
InlinePrimitiveDescriptor("kotlinx.serialization.json.JsonUnquotedLiteral", String.serializer())

Expand Down
Expand Up @@ -11,10 +11,8 @@ import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlin.native.concurrent.*

@SharedImmutable
internal val JsonDeserializationNamesKey = DescriptorSchemaCache.Key<Map<String, Int>>()

@SharedImmutable
internal val JsonSerializationNamesKey = DescriptorSchemaCache.Key<Array<String>>()

private fun SerialDescriptor.buildDeserializationNamesMap(json: Json): Map<String, Int> {
Expand Down
Expand Up @@ -12,7 +12,6 @@ import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlin.native.concurrent.*

@SharedImmutable
private val unsignedNumberDescriptors = setOf(
UInt.serializer().descriptor,
ULong.serializer().descriptor,
Expand Down
Expand Up @@ -13,7 +13,6 @@ private fun toHexChar(i: Int) : Char {
}

@PublishedApi
@SharedImmutable
internal val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
for (c in 0..0x1f) {
val c1 = toHexChar(c shr 12)
Expand All @@ -31,7 +30,6 @@ internal val ESCAPE_STRINGS: Array<String?> = arrayOfNulls<String>(93).apply {
this[0x0c] = "\\f"
}

@SharedImmutable
internal val ESCAPE_MARKERS: ByteArray = ByteArray(93).apply {
for (c in 0..0x1f) {
this[c] = 1.toByte()
Expand Down
@@ -0,0 +1,14 @@
/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization.protobuf.internal

/**
* Suppresses Animal Sniffer plugin errors for certain methods.
* Such methods include references to Java 8 methods that are not
* available in Android API, but can be desugared by R8.
*/
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
internal annotation class SuppressAnimalSniffer
Expand Up @@ -433,6 +433,7 @@ public object ProtoBufSchemaGenerator {
}
}

@SuppressAnimalSniffer // Boolean.hashCode(boolean) in compiler-generated hashCode implementation
private data class TypeDefinition(
val descriptor: SerialDescriptor,
val isSynthetic: Boolean = false,
Expand Down
Expand Up @@ -46,7 +46,6 @@ data class SimpleStringInheritor(val s: String, val i: Int) : SimpleAbstract()
@Serializable
data class PolyBox(@Polymorphic val boxed: SimpleAbstract)

@SharedImmutable
val SimplePolymorphicModule = SerializersModule {
polymorphic(SimpleAbstract::class) {
subclass(SimpleIntInheritor.serializer())
Expand Down
Expand Up @@ -5,8 +5,4 @@
package kotlinx.serialization.test


import kotlin.native.concurrent.SharedImmutable


@SharedImmutable
public actual val currentPlatform: Platform = Platform.NATIVE

0 comments on commit d192d24

Please sign in to comment.