Files
kotlin-fork/compiler/testData/codegen/boxInline/contracts/cfgDependendValInitialization.kt
T
Alexander Udalov 66e19b13ce IR: create shared variables for val-variables when needed
This is possible when a lambda's contract guarantees initialization of a
variable.
2019-11-07 15:20:34 +01:00

41 lines
747 B
Kotlin
Vendored

// !LANGUAGE: +AllowContractsForCustomFunctions +UseCallsInPlaceEffect +ReadDeserializedContracts
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND: NATIVE
// FILE: 1.kt
package test
import kotlin.contracts.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> myrun(block: () -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
// FILE: 2.kt
import test.*
fun test(b: Boolean): Int {
val x: Int
if (b) {
x = 1
} else {
myrun {
x = -1
}
}
return x
}
fun box(): String {
if (test(true) != 1) return "Fail 1"
if (test(false) != -1) return "Fail 2"
return "OK"
}