From 37c3d8f20472ab1e463b2fe31d24599ebbd32c90 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 13 Apr 2017 22:08:33 +0300 Subject: [PATCH] Sort out warnings related to inline and library functions in stdlib-js --- js/js.libraries/src/core/builtins.kt | 2 ++ js/js.libraries/src/core/collections.kt | 4 ++- .../src/core/generated/_ArraysJs.kt | 25 +++++++++++++++++++ js/js.libraries/src/core/numberConversions.kt | 2 ++ js/js.libraries/src/core/ranges.kt | 1 + js/js.libraries/src/core/string.kt | 5 ++++ js/js.libraries/src/core/stringsCode.kt | 8 +++--- .../kotlin-stdlib-gen/src/templates/Arrays.kt | 10 ++++++-- .../src/templates/SpecialJS.kt | 2 +- .../src/templates/SpecialJVM.kt | 2 +- .../src/templates/engine/Engine.kt | 2 +- 11 files changed, 54 insertions(+), 9 deletions(-) diff --git a/js/js.libraries/src/core/builtins.kt b/js/js.libraries/src/core/builtins.kt index 45ccc159164..6094ef38d4b 100644 --- a/js/js.libraries/src/core/builtins.kt +++ b/js/js.libraries/src/core/builtins.kt @@ -155,6 +155,7 @@ internal class BoxedChar(val c: Char) : Comparable { } } +@kotlin.internal.InlineOnly internal inline fun concat(args: Array): T { val typed = js("Array")(args.size) for (i in 0..args.size - 1) { @@ -221,6 +222,7 @@ internal fun charArrayOf() = withType("CharArray", js("new Uint16Array([].slice. internal fun longArrayOf() = withType("LongArray", js("[].slice.call(arguments)")) @JsName("withType") +@kotlin.internal.InlineOnly internal inline fun withType(type: String, array: dynamic): dynamic { array.`$type$` = type return array diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index 9bfea76fe4a..927f78ec018 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -19,12 +19,14 @@ package kotlin.collections import kotlin.comparisons.naturalOrder /** Returns the array if it's not `null`, or an empty array otherwise. */ +@kotlin.internal.InlineOnly public inline fun Array?.orEmpty(): Array = this ?: emptyArray() -@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") +@kotlin.internal.InlineOnly public inline fun Collection.toTypedArray(): Array = copyToArray(this) @JsName("copyToArray") +@PublishedApi internal fun copyToArray(collection: Collection): Array { return if (collection.asDynamic().toArray !== undefined) collection.asDynamic().toArray().unsafeCast>() diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index ed8bbffb334..22541397eff 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -5168,6 +5168,7 @@ public fun CharArray.sortedWith(comparator: Comparator): List { */ @SinceKotlin("1.1") @library("arrayDeepEquals") +@Suppress("UNUSED_PARAMETER") public infix fun Array.contentDeepEquals(other: Array): Boolean { definedExternally } @@ -5203,6 +5204,7 @@ public fun Array.contentDeepToString(): String { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun Array.contentEquals(other: Array): Boolean { definedExternally } @@ -5213,6 +5215,7 @@ public infix fun Array.contentEquals(other: Array): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun ByteArray.contentEquals(other: ByteArray): Boolean { definedExternally } @@ -5223,6 +5226,7 @@ public infix fun ByteArray.contentEquals(other: ByteArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun ShortArray.contentEquals(other: ShortArray): Boolean { definedExternally } @@ -5233,6 +5237,7 @@ public infix fun ShortArray.contentEquals(other: ShortArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun IntArray.contentEquals(other: IntArray): Boolean { definedExternally } @@ -5243,6 +5248,7 @@ public infix fun IntArray.contentEquals(other: IntArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun LongArray.contentEquals(other: LongArray): Boolean { definedExternally } @@ -5253,6 +5259,7 @@ public infix fun LongArray.contentEquals(other: LongArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun FloatArray.contentEquals(other: FloatArray): Boolean { definedExternally } @@ -5263,6 +5270,7 @@ public infix fun FloatArray.contentEquals(other: FloatArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { definedExternally } @@ -5273,6 +5281,7 @@ public infix fun DoubleArray.contentEquals(other: DoubleArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { definedExternally } @@ -5283,6 +5292,7 @@ public infix fun BooleanArray.contentEquals(other: BooleanArray): Boolean { */ @SinceKotlin("1.1") @library("arrayEquals") +@Suppress("UNUSED_PARAMETER") public infix fun CharArray.contentEquals(other: CharArray): Boolean { definedExternally } @@ -12926,6 +12936,7 @@ public fun Array.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun ByteArray.asList(): List { return this.unsafeCast>().asList() } @@ -12933,6 +12944,7 @@ public inline fun ByteArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun ShortArray.asList(): List { return this.unsafeCast>().asList() } @@ -12940,6 +12952,7 @@ public inline fun ShortArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun IntArray.asList(): List { return this.unsafeCast>().asList() } @@ -12947,6 +12960,7 @@ public inline fun IntArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun LongArray.asList(): List { return this.unsafeCast>().asList() } @@ -12954,6 +12968,7 @@ public inline fun LongArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun FloatArray.asList(): List { return this.unsafeCast>().asList() } @@ -12961,6 +12976,7 @@ public inline fun FloatArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun DoubleArray.asList(): List { return this.unsafeCast>().asList() } @@ -12968,6 +12984,7 @@ public inline fun DoubleArray.asList(): List { /** * Returns a [List] that wraps the original array. */ +@kotlin.internal.InlineOnly public inline fun BooleanArray.asList(): List { return this.unsafeCast>().asList() } @@ -13533,6 +13550,7 @@ public fun CharArray.toTypedArray(): Array { /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int): Unit { asDynamic().sort(comparison) } @@ -13540,6 +13558,7 @@ public inline fun Array.sort(noinline comparison: (a: T, b: T) -> Int /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit { asDynamic().sort(comparison) } @@ -13547,6 +13566,7 @@ public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int) /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit { asDynamic().sort(comparison) } @@ -13554,6 +13574,7 @@ public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> I /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit { asDynamic().sort(comparison) } @@ -13561,6 +13582,7 @@ public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): U /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit { asDynamic().sort(comparison) } @@ -13568,6 +13590,7 @@ public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int) /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit { asDynamic().sort(comparison) } @@ -13575,6 +13598,7 @@ public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> I /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit { asDynamic().sort(comparison) } @@ -13582,6 +13606,7 @@ public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) - /** * Sorts the array in-place according to the order specified by the given [comparison] function. */ +@kotlin.internal.InlineOnly public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit { asDynamic().sort(comparison) } diff --git a/js/js.libraries/src/core/numberConversions.kt b/js/js.libraries/src/core/numberConversions.kt index 64c7e8e3c43..9810ab1e718 100644 --- a/js/js.libraries/src/core/numberConversions.kt +++ b/js/js.libraries/src/core/numberConversions.kt @@ -80,6 +80,7 @@ public fun String.toDouble(): Double = (+(this.asDynamic())).unsafeCast( * Parses the string as a [Float] number and returns the result. * @throws NumberFormatException if the string is not a valid representation of a number. */ +@kotlin.internal.InlineOnly public inline fun String.toFloat(): Float = toDouble().unsafeCast() /** @@ -94,6 +95,7 @@ public fun String.toDoubleOrNull(): Double? = (+(this.asDynamic())).unsafeCast() diff --git a/js/js.libraries/src/core/ranges.kt b/js/js.libraries/src/core/ranges.kt index 11f4d08a130..d287a2a5a65 100644 --- a/js/js.libraries/src/core/ranges.kt +++ b/js/js.libraries/src/core/ranges.kt @@ -17,5 +17,6 @@ package kotlin.ranges @SinceKotlin("1.1") +@kotlin.internal.InlineOnly public inline operator fun Float.rangeTo(that: Float): ClosedFloatingPointRange = this.toDouble().rangeTo(that.toDouble()).unsafeCast>() diff --git a/js/js.libraries/src/core/string.kt b/js/js.libraries/src/core/string.kt index b7af96358fe..e1309830100 100644 --- a/js/js.libraries/src/core/string.kt +++ b/js/js.libraries/src/core/string.kt @@ -8,12 +8,16 @@ public inline fun String.toUpperCase(): String = asDynamic().toUpperCase() @kotlin.internal.InlineOnly public inline fun String.toLowerCase(): String = asDynamic().toLowerCase() +@kotlin.internal.InlineOnly internal inline fun String.nativeIndexOf(str: String, fromIndex: Int): Int = asDynamic().indexOf(str, fromIndex) +@kotlin.internal.InlineOnly internal inline fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int = asDynamic().lastIndexOf(str, fromIndex) +@kotlin.internal.InlineOnly internal inline fun String.nativeStartsWith(s: String, position: Int): Boolean = asDynamic().startsWith(s, position) +@kotlin.internal.InlineOnly internal inline fun String.nativeEndsWith(s: String): Boolean = asDynamic().endsWith(s) @kotlin.internal.InlineOnly @@ -34,4 +38,5 @@ public inline fun String.match(regex: String): Array? = asDynamic().matc @kotlin.internal.InlineOnly public inline val CharSequence.size: Int get() = asDynamic().length +@kotlin.internal.InlineOnly internal inline fun String.nativeReplace(pattern: RegExp, replacement: String): String = asDynamic().replace(pattern, replacement) diff --git a/js/js.libraries/src/core/stringsCode.kt b/js/js.libraries/src/core/stringsCode.kt index bf4804d1285..eb0f18589cf 100644 --- a/js/js.libraries/src/core/stringsCode.kt +++ b/js/js.libraries/src/core/stringsCode.kt @@ -2,7 +2,9 @@ package kotlin.text import kotlin.js.RegExp +@kotlin.internal.InlineOnly internal inline fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int = nativeIndexOf(ch.toString(), fromIndex) +@kotlin.internal.InlineOnly internal inline fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int = nativeLastIndexOf(ch.toString(), fromIndex) /** @@ -37,7 +39,7 @@ public fun String.endsWith(suffix: String, ignoreCase: Boolean = false): Boolean -public inline fun String.matches(regex: String): Boolean { +public fun String.matches(regex: String): Boolean { val result = this.match(regex) return result != null && result.size != 0 } @@ -62,7 +64,7 @@ public fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, othe * * @includeFunctionBody ../../test/StringTest.kt capitalize */ -public inline fun String.capitalize(): String { +public fun String.capitalize(): String { return if (isNotEmpty()) substring(0, 1).toUpperCase() + substring(1) else this } @@ -71,7 +73,7 @@ public inline fun String.capitalize(): String { * * @includeFunctionBody ../../test/StringTest.kt decapitalize */ -public inline fun String.decapitalize(): String { +public fun String.decapitalize(): String { return if (isNotEmpty()) substring(0, 1).toLowerCase() + substring(1) else this } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index e0bfb66aa66..4214756e7b4 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -58,7 +58,10 @@ fun arrays(): List { returns("Boolean") body(Platform.JVM) { "return java.util.Arrays.equals(this, other)" } - annotations(Platform.JS, """@library("arrayEquals")""") + annotations(Platform.JS, """ + @library("arrayEquals") + @Suppress("UNUSED_PARAMETER") + """) body(Platform.JS) { "definedExternally" } } @@ -79,7 +82,10 @@ fun arrays(): List { } returns("Boolean") body(Platform.JVM) { "return java.util.Arrays.deepEquals(this, other)" } - annotations(Platform.JS, """@library("arrayDeepEquals")""") + annotations(Platform.JS, """ + @library("arrayDeepEquals") + @Suppress("UNUSED_PARAMETER") + """) body(Platform.JS) { "definedExternally" } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index cea96e9363c..e985483d077 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -24,7 +24,7 @@ fun specialJS(): List { templates add f("sort(noinline comparison: (a: T, b: T) -> Int)") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) - inline(true) + inline(Inline.Only) returns("Unit") doc { "Sorts the array in-place according to the order specified by the given [comparison] function." } body { "asDynamic().sort(comparison)" } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 1f77104a390..6ebaa5cca2e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -497,7 +497,7 @@ object CommonArrays { body(Platform.JS) { objectLiteralImpl } } else { - inline(Platform.JS, Inline.Yes, ArraysOfPrimitives) + inline(Platform.JS, Inline.Only, ArraysOfPrimitives) body(Platform.JS) { "return this.unsafeCast>().asList()" } } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt index d2248b9ab53..896569e59a7 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/engine/Engine.kt @@ -483,7 +483,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") { builder.append("@SinceKotlin(\"$since\")\n") } - annotations[platform, f]?.let { builder.append(it).append('\n') } + annotations[platform, f]?.let { builder.append(it.trimIndent()).append('\n') } if (inline[platform, f] == Inline.Only) { builder.append("@kotlin.internal.InlineOnly").append('\n')