[stdlib] Specify AT_MOST_ONCE contract for default-value-like functional parameters KT-54879

This commit is contained in:
Ilya Gorbunov
2024-03-04 08:26:58 +01:00
committed by Space Team
parent dee9fdc02d
commit fc13ae7b4d
21 changed files with 167 additions and 14 deletions
@@ -13,6 +13,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -649,6 +650,9 @@ public expect fun CharArray.elementAt(index: Int): Char
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -659,6 +663,9 @@ public inline fun <T> Array<out T>.elementAtOrElse(index: Int, defaultValue: (In
*/
@kotlin.internal.InlineOnly
public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -669,6 +676,9 @@ public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> B
*/
@kotlin.internal.InlineOnly
public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -679,6 +689,9 @@ public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
*/
@kotlin.internal.InlineOnly
public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -689,6 +702,9 @@ public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> In
*/
@kotlin.internal.InlineOnly
public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -699,6 +715,9 @@ public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> L
*/
@kotlin.internal.InlineOnly
public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -709,6 +728,9 @@ public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
*/
@kotlin.internal.InlineOnly
public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -719,6 +741,9 @@ public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
*/
@kotlin.internal.InlineOnly
public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -729,6 +754,9 @@ public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -
*/
@kotlin.internal.InlineOnly
public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1352,6 +1380,9 @@ public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? {
*/
@kotlin.internal.InlineOnly
public inline fun <T> Array<out T>.getOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1360,6 +1391,9 @@ public inline fun <T> Array<out T>.getOrElse(index: Int, defaultValue: (Int) ->
*/
@kotlin.internal.InlineOnly
public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1368,6 +1402,9 @@ public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte):
*/
@kotlin.internal.InlineOnly
public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1376,6 +1413,9 @@ public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short)
*/
@kotlin.internal.InlineOnly
public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1384,6 +1424,9 @@ public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): In
*/
@kotlin.internal.InlineOnly
public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1392,6 +1435,9 @@ public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long):
*/
@kotlin.internal.InlineOnly
public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1400,6 +1446,9 @@ public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float)
*/
@kotlin.internal.InlineOnly
public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1408,6 +1457,9 @@ public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Doubl
*/
@kotlin.internal.InlineOnly
public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -1416,6 +1468,9 @@ public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Bool
*/
@kotlin.internal.InlineOnly
public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -13,6 +13,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -103,6 +104,9 @@ public inline fun <T> List<T>.elementAt(index: Int): T {
* @sample samples.collections.Collections.Elements.elementAtOrElse
*/
public fun <T> Iterable<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
if (this is List)
return this.getOrElse(index, defaultValue)
if (index < 0)
@@ -124,6 +128,9 @@ public fun <T> Iterable<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T)
*/
@kotlin.internal.InlineOnly
public inline fun <T> List<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -294,6 +301,9 @@ public inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T? {
*/
@kotlin.internal.InlineOnly
public inline fun <T> List<T>.getOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -13,6 +13,7 @@ package kotlin.comparisons
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -13,6 +13,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -13,6 +13,7 @@ package kotlin.ranges
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -13,6 +13,7 @@ package kotlin.sequences
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -43,6 +44,9 @@ public fun <T> Sequence<T>.elementAt(index: Int): T {
* @sample samples.collections.Collections.Elements.elementAtOrElse
*/
public fun <T> Sequence<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
if (index < 0)
return defaultValue(index)
val iterator = iterator()
@@ -13,6 +13,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -13,6 +13,7 @@ package kotlin.text
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -29,6 +30,9 @@ public expect fun CharSequence.elementAt(index: Int): Char
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -132,6 +136,9 @@ public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char?
*/
@kotlin.internal.InlineOnly
public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -14,6 +14,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -323,6 +324,9 @@ public expect fun UShortArray.elementAt(index: Int): UShort
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UInt): UInt {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -335,6 +339,9 @@ public inline fun UIntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> U
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> ULong): ULong {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -347,6 +354,9 @@ public inline fun ULongArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UByte): UByte {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -359,6 +369,9 @@ public inline fun UByteArray.elementAtOrElse(index: Int, defaultValue: (Int) ->
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> UShort): UShort {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -689,6 +702,9 @@ public inline fun UShortArray.firstOrNull(predicate: (UShort) -> Boolean): UShor
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt): UInt {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -699,6 +715,9 @@ public inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt):
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong): ULong {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -709,6 +728,9 @@ public inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong)
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte): UByte {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -719,6 +741,9 @@ public inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte)
@ExperimentalUnsignedTypes
@kotlin.internal.InlineOnly
public inline fun UShortArray.getOrElse(index: Int, defaultValue: (Int) -> UShort): UShort {
contract {
callsInPlace(defaultValue, InvocationKind.AT_MOST_ONCE)
}
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
@@ -13,6 +13,7 @@ package kotlin.collections
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
import kotlin.ranges.contains
import kotlin.ranges.reversed
@@ -13,6 +13,7 @@ package kotlin.comparisons
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -13,6 +13,7 @@ package kotlin.ranges
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**
@@ -13,6 +13,7 @@ package kotlin.sequences
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import kotlin.contracts.*
import kotlin.random.*
/**