Files
kotlin-fork/compiler/testData/diagnostics/tests/when/withSubjectVariable/capturingInInitializer.kt
T
2018-06-20 14:06:34 +03:00

37 lines
915 B
Kotlin
Vendored

// !LANGUAGE: +VariableDeclarationInWhenSubject
// !DIAGNOSTICS: -UNUSED_VARIABLE
fun getBoolean() = true
fun testSafeCaptureVarInInitializer() {
var x: Int? = 42
x!!
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
val s = when (val y = run { x = 42; 32 }) {
0 -> {
<!SMARTCAST_IMPOSSIBLE!>x<!>.inc() // TODO fix smart casts for captured variables
"0"
}
else -> "!= 0"
}
<!SMARTCAST_IMPOSSIBLE!>x<!>.inc() // TODO fix smart casts for captured variables
}
fun testUnsafeCaptureVarInInitializer() {
var x: Int? = 42
x!!
<!DEBUG_INFO_SMARTCAST!>x<!>.inc()
val s = when (val y = run { x = null; 32 }) {
0 -> {
<!SMARTCAST_IMPOSSIBLE!>x<!>.inc() // NB smart cast should be impossible
"0"
}
else -> "!= 0"
}
<!SMARTCAST_IMPOSSIBLE!>x<!>.inc() // NB smart cast should be impossible
}