Extract effect from lambda argument if it is in parentheses

Otherwise, contracts on the parameter have no effect.
 #KT-42044 Fixed
 #KT-26229 Fixed
This commit is contained in:
Ilmir Usmanov
2020-10-11 20:07:05 +02:00
parent df64bb3eb7
commit 3b5706972e
15 changed files with 79 additions and 76 deletions
@@ -0,0 +1,27 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: NATIVE
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
import kotlin.contracts.*
class Smth {
val whatever: Int
init {
calculate({ whatever = it })
}
@OptIn(ExperimentalContracts::class)
private inline fun calculate(block: (Int) -> Unit) {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
block(42)
}
}
fun box(): String {
val smth = Smth()
return if (smth.whatever == 42) "OK" else "FAIL ${smth.whatever}"
}