From c51fa3d0fee7b108318c2c669b4eb5543f162bf2 Mon Sep 17 00:00:00 2001 From: Dustin Lam Date: Thu, 15 Aug 2024 17:10:57 -0700 Subject: [PATCH] K2 fixes for collection The build still fails despite these changes due to workaround from KT-29963 no longer working. Test: ./gradlew collection:collection:build -Pandroidx.forceKotlin20Target=true Change-Id: I67ff4141cdbd8f9a2eade58598b45e03b672f4d5 --- .../kotlin/androidx/collection/ArraySet.kt | 3 +-- .../androidx/collection/LongSparseArray.kt | 18 +----------------- .../androidx/collection/SparseArrayCompat.kt | 18 +----------------- .../androidx/collection/internal/LockExt.kt | 1 - .../androidx/collection/internal/Lock.jvm.kt | 5 +++-- .../collection/internal/LruHashMap.jvm.kt | 1 - .../{Lock.mingw.kt => LockImpl.mingw.kt} | 0 .../androidx/collection/ArraySet.native.kt | 2 +- .../CollectionPlatformUtils.native.kt | 1 + .../collection/LongSparseArray.native.kt | 5 +---- .../collection/SparseArrayCompat.native.kt | 5 +---- .../collection/internal/Lock.native.kt | 10 +++++++--- .../collection/internal/LruHashMap.native.kt | 1 - .../{Lock.unix.kt => LockImpl.unix.kt} | 0 14 files changed, 17 insertions(+), 53 deletions(-) rename collection/collection/src/mingwMain/kotlin/androidx/collection/internal/{Lock.mingw.kt => LockImpl.mingw.kt} (100%) rename collection/collection/src/unixMain/kotlin/androidx/collection/internal/{Lock.unix.kt => LockImpl.unix.kt} (100%) diff --git a/collection/collection/src/commonMain/kotlin/androidx/collection/ArraySet.kt b/collection/collection/src/commonMain/kotlin/androidx/collection/ArraySet.kt index 2aa9a4aae47ec..e1566fbc4cd83 100644 --- a/collection/collection/src/commonMain/kotlin/androidx/collection/ArraySet.kt +++ b/collection/collection/src/commonMain/kotlin/androidx/collection/ArraySet.kt @@ -19,7 +19,6 @@ package androidx.collection import androidx.collection.internal.EMPTY_INTS import androidx.collection.internal.EMPTY_OBJECTS import androidx.collection.internal.binarySearch -import kotlin.jvm.JvmOverloads /** Returns an empty new [ArraySet]. */ @Suppress("NOTHING_TO_INLINE") // Alias to public API. @@ -58,7 +57,7 @@ public fun arraySetOf(vararg values: T): ArraySet { * @constructor Creates a new empty ArraySet. The default capacity of an array map is 0, and will * grow once items are added to it. */ -public expect class ArraySet @JvmOverloads constructor(capacity: Int = 0) : +public expect class ArraySet constructor(capacity: Int = 0) : MutableCollection, MutableSet { internal var hashes: IntArray diff --git a/collection/collection/src/commonMain/kotlin/androidx/collection/LongSparseArray.kt b/collection/collection/src/commonMain/kotlin/androidx/collection/LongSparseArray.kt index 732c229845335..d776f9eb82b70 100644 --- a/collection/collection/src/commonMain/kotlin/androidx/collection/LongSparseArray.kt +++ b/collection/collection/src/commonMain/kotlin/androidx/collection/LongSparseArray.kt @@ -20,9 +20,6 @@ import androidx.collection.internal.binarySearch import androidx.collection.internal.idealLongArraySize import androidx.collection.internal.requirePrecondition import kotlin.DeprecationLevel.HIDDEN -import kotlin.jvm.JvmField -import kotlin.jvm.JvmOverloads -import kotlin.jvm.JvmSynthetic private val DELETED = Any() @@ -54,23 +51,10 @@ private val DELETED = Any() * initial capacity of 0, the sparse array will be initialized with a light-weight representation * not requiring any additional array allocations. */ -public expect open class LongSparseArray -@JvmOverloads -public constructor(initialCapacity: Int = 10) { - @JvmSynthetic // Hide from Java callers. - @JvmField +public expect open class LongSparseArray public constructor(initialCapacity: Int = 10) { internal var garbage: Boolean - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var keys: LongArray - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var values: Array - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var size: Int /** diff --git a/collection/collection/src/commonMain/kotlin/androidx/collection/SparseArrayCompat.kt b/collection/collection/src/commonMain/kotlin/androidx/collection/SparseArrayCompat.kt index fb5a08e5f4c64..4e0d279353cb6 100644 --- a/collection/collection/src/commonMain/kotlin/androidx/collection/SparseArrayCompat.kt +++ b/collection/collection/src/commonMain/kotlin/androidx/collection/SparseArrayCompat.kt @@ -18,9 +18,6 @@ package androidx.collection import androidx.collection.internal.binarySearch import androidx.collection.internal.idealIntArraySize -import kotlin.jvm.JvmField -import kotlin.jvm.JvmOverloads -import kotlin.jvm.JvmSynthetic import kotlin.math.min private val DELETED = Any() @@ -57,23 +54,10 @@ private val DELETED = Any() * initial capacity of 0, the sparse array will be initialized with a light-weight representation * not requiring any additional array allocations. */ -public expect open class SparseArrayCompat -@JvmOverloads -public constructor(initialCapacity: Int = 10) { - @JvmSynthetic // Hide from Java callers. - @JvmField +public expect open class SparseArrayCompat public constructor(initialCapacity: Int = 10) { internal var garbage: Boolean - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var keys: IntArray - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var values: Array - - @JvmSynthetic // Hide from Java callers. - @JvmField internal var size: Int /** diff --git a/collection/collection/src/commonMain/kotlin/androidx/collection/internal/LockExt.kt b/collection/collection/src/commonMain/kotlin/androidx/collection/internal/LockExt.kt index 61e384ae04954..16ba57657ae19 100644 --- a/collection/collection/src/commonMain/kotlin/androidx/collection/internal/LockExt.kt +++ b/collection/collection/src/commonMain/kotlin/androidx/collection/internal/LockExt.kt @@ -21,6 +21,5 @@ import kotlin.contracts.contract internal inline fun Lock.synchronized(block: () -> T): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return synchronizedImpl(block) } diff --git a/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/Lock.jvm.kt b/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/Lock.jvm.kt index 1be5d51b99508..82591d2ab04c3 100644 --- a/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/Lock.jvm.kt +++ b/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/Lock.jvm.kt @@ -22,6 +22,7 @@ import androidx.annotation.RestrictTo internal actual class Lock { - actual inline fun synchronizedImpl(block: () -> T): T = - synchronized(lock = this, block = block) + actual inline fun synchronizedImpl(block: () -> T): T { + return synchronized(lock = this, block = block) + } } diff --git a/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/LruHashMap.jvm.kt b/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/LruHashMap.jvm.kt index c2fe91c5f8a37..5e526fbad348d 100644 --- a/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/LruHashMap.jvm.kt +++ b/collection/collection/src/jvmMain/kotlin/androidx/collection/internal/LruHashMap.jvm.kt @@ -20,7 +20,6 @@ package androidx.collection.internal import androidx.annotation.RestrictTo -@Suppress("ACTUAL_WITHOUT_EXPECT") // https://youtrack.jetbrains.com/issue/KT-37316 internal actual class LruHashMap actual constructor( initialCapacity: Int, diff --git a/collection/collection/src/mingwMain/kotlin/androidx/collection/internal/Lock.mingw.kt b/collection/collection/src/mingwMain/kotlin/androidx/collection/internal/LockImpl.mingw.kt similarity index 100% rename from collection/collection/src/mingwMain/kotlin/androidx/collection/internal/Lock.mingw.kt rename to collection/collection/src/mingwMain/kotlin/androidx/collection/internal/LockImpl.mingw.kt diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/ArraySet.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/ArraySet.native.kt index 3adc4c57f01fd..607d6ba8cd063 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/ArraySet.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/ArraySet.native.kt @@ -43,7 +43,7 @@ import androidx.collection.internal.EMPTY_OBJECTS * grow once items are added to it. */ // JvmOverloads is required on constructor to match expect declaration -public actual class ArraySet @kotlin.jvm.JvmOverloads actual constructor(capacity: Int) : +public actual class ArraySet actual constructor(capacity: Int) : MutableCollection, MutableSet { internal actual var hashes: IntArray = EMPTY_INTS diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/CollectionPlatformUtils.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/CollectionPlatformUtils.native.kt index 2035fec6b0ce3..d991a24925a09 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/CollectionPlatformUtils.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/CollectionPlatformUtils.native.kt @@ -18,6 +18,7 @@ package androidx.collection /** Native actual of internal utils for handling target differences in collection code. */ internal actual object CollectionPlatformUtils { + @Suppress("NOTHING_TO_INLINE") internal actual inline fun createIndexOutOfBoundsException(): IndexOutOfBoundsException { return IndexOutOfBoundsException() } diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/LongSparseArray.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/LongSparseArray.native.kt index e66b4fe1875da..d118e16676486 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/LongSparseArray.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/LongSparseArray.native.kt @@ -48,10 +48,7 @@ import androidx.collection.internal.idealLongArraySize * initial capacity of 0, the sparse array will be initialized with a light-weight representation * not requiring any additional array allocations. */ -public actual open class LongSparseArray -// JvmOverloads is required on constructor to match expect declaration -@kotlin.jvm.JvmOverloads -public actual constructor(initialCapacity: Int) { +public actual open class LongSparseArray public actual constructor(initialCapacity: Int) { internal actual var garbage = false internal actual var keys: LongArray internal actual var values: Array diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/SparseArrayCompat.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/SparseArrayCompat.native.kt index 39b56d63bdfed..974ebd4fd594b 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/SparseArrayCompat.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/SparseArrayCompat.native.kt @@ -52,10 +52,7 @@ import androidx.collection.internal.idealIntArraySize * initial capacity of 0, the sparse array will be initialized with a light-weight representation * not requiring any additional array allocations. */ -public actual open class SparseArrayCompat -// JvmOverloads is required on constructor to match expect declaration -@kotlin.jvm.JvmOverloads -public actual constructor(initialCapacity: Int) { +public actual open class SparseArrayCompat public actual constructor(initialCapacity: Int) { internal actual var garbage = false internal actual var keys: IntArray internal actual var values: Array diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/Lock.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/Lock.native.kt index fcb8eb0dcc07b..519ac12593b05 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/Lock.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/Lock.native.kt @@ -16,7 +16,10 @@ package androidx.collection.internal -import kotlin.native.internal.createCleaner +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract +import kotlin.experimental.ExperimentalNativeApi +import kotlin.native.ref.createCleaner /** * Wrapper for platform.posix.PTHREAD_MUTEX_RECURSIVE which is represented as kotlin.Int on darwin @@ -32,16 +35,17 @@ internal expect class LockImpl() { internal fun destroy() } -@Suppress("ACTUAL_WITHOUT_EXPECT") // https://youtrack.jetbrains.com/issue/KT-37316 internal actual class Lock actual constructor() { private val lockImpl = LockImpl() @Suppress("unused") // The returned Cleaner must be assigned to a property - @ExperimentalStdlibApi + @OptIn(ExperimentalNativeApi::class) private val cleaner = createCleaner(lockImpl, LockImpl::destroy) actual inline fun synchronizedImpl(block: () -> T): T { + contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } + lock() return try { block() diff --git a/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/LruHashMap.native.kt b/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/LruHashMap.native.kt index e7715d6888458..27f4305d10321 100644 --- a/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/LruHashMap.native.kt +++ b/collection/collection/src/nativeMain/kotlin/androidx/collection/internal/LruHashMap.native.kt @@ -16,7 +16,6 @@ package androidx.collection.internal -@Suppress("ACTUAL_WITHOUT_EXPECT") // https://youtrack.jetbrains.com/issue/KT-37316 internal actual class LruHashMap actual constructor( initialCapacity: Int, diff --git a/collection/collection/src/unixMain/kotlin/androidx/collection/internal/Lock.unix.kt b/collection/collection/src/unixMain/kotlin/androidx/collection/internal/LockImpl.unix.kt similarity index 100% rename from collection/collection/src/unixMain/kotlin/androidx/collection/internal/Lock.unix.kt rename to collection/collection/src/unixMain/kotlin/androidx/collection/internal/LockImpl.unix.kt