Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/controlflow/flowInlining/breakContinuesInInlinedLambda.kt
T
Dmitry Savvinov 4434db4d69 Effects: add diagnostic tests on contracts
- Make AbstractDiagnosticsTest dump function contracts
- Add diagnostics tests on parsing contracts
- Add diagnostics tests on smartcats in presence of functions with
contracts
- Add diagnostics tests on initialization and flow in presence of
in-place called lambdas

==========
Introduction of EffectSystem: 16/18
2017-10-12 11:55:26 +03:00

50 lines
1.3 KiB
Kotlin
Vendored

// !LANGUAGE: +CallsInPlaceEffect
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.internal.contracts.*
inline fun <T> myRun(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block()
}
fun getBoolean(): Boolean = false
fun test() {
val x: Int
if (getBoolean())
myRun {
while (getBoolean()) {
do {
myRun {
if (getBoolean()) {
x = 42 // No reassignment because of break
}
else {
x = 43 // No reassignment because of break
}
}
break
} while (<!UNREACHABLE_CODE!>getBoolean()<!>)
// Loop executed exectly once, initializing x
myRun { x.inc() }
myRun { <!VAL_REASSIGNMENT!>x<!> = 42 }
break
}
// x is I?D here because loop could've been execited
// VAL_REASSIGNMENT isn't reported because of repeating diagnostic
x = 42
// x is ID now
}
else
myRun {
x = 42
}
// x is ID because both branches are ID
x.inc()
}