// !DUMP_CFG import kotlin.contracts.* fun n(): T? = null @OptIn(ExperimentalContracts::class) fun run2(x: () -> T, y: () -> T) { contract { callsInPlace(x, InvocationKind.EXACTLY_ONCE) callsInPlace(y, InvocationKind.EXACTLY_ONCE) } x() y() } fun test1(x: String?) { var p = x if (p != null) { run2( { p = null; n() }, { p.length; 123 } // Bad: may or may not not be called first ) p.length // Bad: p = null } } fun test1_tail(x: String?) { var p = x if (p != null) { run2({ p = null; n() }) { p.length // Bad: may or may not not be called first 123 } p.length // Bad: p = null } } fun test2(x: Any?) { var p: Any? = x p.length // Bad run2({ p = null; n() }, { p as String; 123 }) p.length // Bad: p is Nothing? | (String & Nothing?) = Nothing? p?.length // Technically OK because p is null, but what is "length"? } fun test3(x: Any?) { var p: Any? = x p.length // Bad run2({ p = null; n() }, { p = ""; 123 }) p.length // Bad: p can be null p?.length // Bad: KT-37838 -> OK: p is String | Nothing? = String? } interface I1 { val x: Int } interface I2 { val y: Int } fun test4(x: Any?) { x.x // Bad x.y // Bad run2( { x as I1; x.y; n() }, // Bad: may or may not be called first { x as I2; x.x; 123 } // Bad: may or may not be called first ) x.x // Bad: KT-37838 -> OK: x is I1 & I2 x.y // Bad: KT-37838 -> OK: x is I1 & I2 } fun test5(x: Any?, q: String?) { var p: Any? = x p.length // Bad run2({ p as Int; 123 }, { p = q; n() }) p.length // Bad: p is String? | (String? & Int) = String? p?.length // Bad: KT-37838 -> OK: p is String? } fun test6() { val x: String // not necessarily initialized in second lambda (may call in any order) run2({ x = ""; x.length }, { x.length }) x.length // initialized here } fun test7() { val x: Any? = "" val y: Any? run2({ y = x }, { }) if (y is String) { x.length // ok - aliased } }