From 974837654e0f92ac0968dd68c6e76da1be1158bf Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 20 Sep 2018 04:06:57 +0300 Subject: [PATCH] Remove workaround for complex default value in Array.copyInto #KT-22818 --- .../stdlib/common/src/generated/_Arrays.kt | 18 +++++++++--------- .../stdlib/common/src/generated/_UArrays.kt | 8 ++++---- libraries/stdlib/js/src/kotlin/collections.kt | 2 -- .../stdlib/jvm/src/generated/_ArraysJvm.kt | 18 ------------------ .../stdlib/test/collections/ArraysTest.kt | 2 +- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 5 +---- 6 files changed, 15 insertions(+), 38 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_Arrays.kt b/libraries/stdlib/common/src/generated/_Arrays.kt index 92c20b6c996..11adb47aa5f 100644 --- a/libraries/stdlib/common/src/generated/_Arrays.kt +++ b/libraries/stdlib/common/src/generated/_Arrays.kt @@ -5797,7 +5797,7 @@ public expect fun CharArray.contentToString(): String * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): Array +public expect fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): Array /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5816,7 +5816,7 @@ public expect fun Array.copyInto(destination: Array, destinationOf * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ByteArray +public expect fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ByteArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5835,7 +5835,7 @@ public expect fun ByteArray.copyInto(destination: ByteArray, destinationOffset: * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ShortArray +public expect fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ShortArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5854,7 +5854,7 @@ public expect fun ShortArray.copyInto(destination: ShortArray, destinationOffset * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): IntArray +public expect fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): IntArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5873,7 +5873,7 @@ public expect fun IntArray.copyInto(destination: IntArray, destinationOffset: In * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): LongArray +public expect fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): LongArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5892,7 +5892,7 @@ public expect fun LongArray.copyInto(destination: LongArray, destinationOffset: * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): FloatArray +public expect fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): FloatArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5911,7 +5911,7 @@ public expect fun FloatArray.copyInto(destination: FloatArray, destinationOffset * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): DoubleArray +public expect fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): DoubleArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5930,7 +5930,7 @@ public expect fun DoubleArray.copyInto(destination: DoubleArray, destinationOffs * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): BooleanArray +public expect fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): BooleanArray /** * Copies this array or its subrange into the [destination] array and returns that array. @@ -5949,7 +5949,7 @@ public expect fun BooleanArray.copyInto(destination: BooleanArray, destinationOf * @return the [destination] array. */ @SinceKotlin("1.3") -public expect fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): CharArray +public expect fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): CharArray /** * Returns new array which is a copy of the original array. diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index c032443c2b7..4236c3e2b24 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -345,7 +345,7 @@ public fun UShortArray.contentToString(): String { @SinceKotlin("1.3") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun UIntArray.copyInto(destination: UIntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UIntArray { +public inline fun UIntArray.copyInto(destination: UIntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): UIntArray { return UIntArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) } @@ -368,7 +368,7 @@ public inline fun UIntArray.copyInto(destination: UIntArray, destinationOffset: @SinceKotlin("1.3") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun ULongArray.copyInto(destination: ULongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): ULongArray { +public inline fun ULongArray.copyInto(destination: ULongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ULongArray { return ULongArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) } @@ -391,7 +391,7 @@ public inline fun ULongArray.copyInto(destination: ULongArray, destinationOffset @SinceKotlin("1.3") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun UByteArray.copyInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UByteArray { +public inline fun UByteArray.copyInto(destination: UByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): UByteArray { return UByteArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) } @@ -414,7 +414,7 @@ public inline fun UByteArray.copyInto(destination: UByteArray, destinationOffset @SinceKotlin("1.3") @ExperimentalUnsignedTypes @kotlin.internal.InlineOnly -public inline fun UShortArray.copyInto(destination: UShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1): UShortArray { +public inline fun UShortArray.copyInto(destination: UShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): UShortArray { return UShortArray(storage.copyInto(destination.storage, destinationOffset, startIndex, endIndex)) } diff --git a/libraries/stdlib/js/src/kotlin/collections.kt b/libraries/stdlib/js/src/kotlin/collections.kt index 33178bcae3f..80ed17f84de 100644 --- a/libraries/stdlib/js/src/kotlin/collections.kt +++ b/libraries/stdlib/js/src/kotlin/collections.kt @@ -130,8 +130,6 @@ internal actual fun arrayOfNulls(reference: Array, size: Int): Array { @PublishedApi @JsName("arrayCopy") internal fun arrayCopy(source: Array, destination: Array, destinationOffset: Int, startIndex: Int, endIndex: Int) { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) source.size else endIndex // TODO: Remove when default value from expect is fixed AbstractList.checkRangeIndexes(startIndex, endIndex, source.size) val rangeSize = endIndex - startIndex AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size) diff --git a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt index c42b738e35b..520c76d0b84 100644 --- a/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt +++ b/libraries/stdlib/jvm/src/generated/_ArraysJvm.kt @@ -628,8 +628,6 @@ public actual inline fun CharArray.contentToString(): String { @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun Array.copyInto(destination: Array, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): Array { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -653,8 +651,6 @@ public actual fun Array.copyInto(destination: Array, destinationOf @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun ByteArray.copyInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ByteArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -678,8 +674,6 @@ public actual fun ByteArray.copyInto(destination: ByteArray, destinationOffset: @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun ShortArray.copyInto(destination: ShortArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): ShortArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -703,8 +697,6 @@ public actual fun ShortArray.copyInto(destination: ShortArray, destinationOffset @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun IntArray.copyInto(destination: IntArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): IntArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -728,8 +720,6 @@ public actual fun IntArray.copyInto(destination: IntArray, destinationOffset: In @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun LongArray.copyInto(destination: LongArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): LongArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -753,8 +743,6 @@ public actual fun LongArray.copyInto(destination: LongArray, destinationOffset: @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun FloatArray.copyInto(destination: FloatArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): FloatArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -778,8 +766,6 @@ public actual fun FloatArray.copyInto(destination: FloatArray, destinationOffset @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun DoubleArray.copyInto(destination: DoubleArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): DoubleArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -803,8 +789,6 @@ public actual fun DoubleArray.copyInto(destination: DoubleArray, destinationOffs @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun BooleanArray.copyInto(destination: BooleanArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): BooleanArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } @@ -828,8 +812,6 @@ public actual fun BooleanArray.copyInto(destination: BooleanArray, destinationOf @SinceKotlin("1.3") @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") public actual fun CharArray.copyInto(destination: CharArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size): CharArray { - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination } diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 06b7d314fa9..618386adf2a 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -854,7 +854,7 @@ class ArraysTest { dest.copyInto(dest, 1, 0, 2) assertTEquals(result3, dest, "Overlapping forward copy: ${result2.toStringT()}, ${dest.toStringT()}") - for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0)) { + for ((start, end) in listOf(-1 to 0, 0 to 4, 4 to 4, 1 to 0, 0 to -1)) { val bounds = "start: $start, end: $end" val ex = assertFails(bounds) { newValues.copyInto(dest, 0, start, end) } assertTrue(ex is IllegalArgumentException || ex is IndexOutOfBoundsException, "Unexpected exception type: $ex") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 22bf23ce456..51b7319a327 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -489,8 +489,7 @@ object ArrayOps : TemplateGroupBase() { } } - // TODO: Remove -1 from common signature - val f_copyInto = fn("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = -1)") { + val f_copyInto = fn("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)") { include(InvariantArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { since("1.3") @@ -530,8 +529,6 @@ object ArrayOps : TemplateGroupBase() { signature("copyInto(destination: SELF, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size)") body { """ - @Suppress("NAME_SHADOWING") - val endIndex = if (endIndex == -1) size else endIndex // TODO: Remove when default value from expect is fixed System.arraycopy(this, startIndex, destination, destinationOffset, endIndex - startIndex) return destination """