Skip to content

Commit

Permalink
K2 fixes for collection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dlam committed Aug 16, 2024
1 parent cd9409b commit c51fa3d
Show file tree
Hide file tree
Showing 14 changed files with 17 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -58,7 +57,7 @@ public fun <T> arraySetOf(vararg values: T): ArraySet<T> {
* @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<E> @JvmOverloads constructor(capacity: Int = 0) :
public expect class ArraySet<E> constructor(capacity: Int = 0) :
MutableCollection<E>, MutableSet<E> {

internal var hashes: IntArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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<E>
@JvmOverloads
public constructor(initialCapacity: Int = 10) {
@JvmSynthetic // Hide from Java callers.
@JvmField
public expect open class LongSparseArray<E> 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<Any?>

@JvmSynthetic // Hide from Java callers.
@JvmField
internal var size: Int

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<E>
@JvmOverloads
public constructor(initialCapacity: Int = 10) {
@JvmSynthetic // Hide from Java callers.
@JvmField
public expect open class SparseArrayCompat<E> 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<Any?>

@JvmSynthetic // Hide from Java callers.
@JvmField
internal var size: Int

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ import kotlin.contracts.contract

internal inline fun <T> Lock.synchronized(block: () -> T): T {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }

return synchronizedImpl(block)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.annotation.RestrictTo

internal actual class Lock {

actual inline fun <T> synchronizedImpl(block: () -> T): T =
synchronized(lock = this, block = block)
actual inline fun <T> synchronizedImpl(block: () -> T): T {
return synchronized(lock = this, block = block)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<K : Any, V : Any>
actual constructor(
initialCapacity: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<E> @kotlin.jvm.JvmOverloads actual constructor(capacity: Int) :
public actual class ArraySet<E> actual constructor(capacity: Int) :
MutableCollection<E>, MutableSet<E> {

internal actual var hashes: IntArray = EMPTY_INTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>
// JvmOverloads is required on constructor to match expect declaration
@kotlin.jvm.JvmOverloads
public actual constructor(initialCapacity: Int) {
public actual open class LongSparseArray<E> public actual constructor(initialCapacity: Int) {
internal actual var garbage = false
internal actual var keys: LongArray
internal actual var values: Array<Any?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>
// JvmOverloads is required on constructor to match expect declaration
@kotlin.jvm.JvmOverloads
public actual constructor(initialCapacity: Int) {
public actual open class SparseArrayCompat<E> public actual constructor(initialCapacity: Int) {
internal actual var garbage = false
internal actual var keys: IntArray
internal actual var values: Array<Any?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <T> synchronizedImpl(block: () -> T): T {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }

lock()
return try {
block()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package androidx.collection.internal

@Suppress("ACTUAL_WITHOUT_EXPECT") // https://youtrack.jetbrains.com/issue/KT-37316
internal actual class LruHashMap<K : Any, V : Any>
actual constructor(
initialCapacity: Int,
Expand Down

0 comments on commit c51fa3d

Please sign in to comment.