Files
kotlin-fork/compiler/testData/diagnostics/testsWithStdLib/contracts/smartcasts/receiver.kt
T
2021-09-10 16:29:16 +03:00

36 lines
733 B
Kotlin
Vendored

// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
// !OPT_IN: kotlin.contracts.ExperimentalContracts
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
import kotlin.contracts.*
fun Any?.isNull(): Boolean {
contract {
returns(false) implies (this@isNull != null)
}
return this == null
}
fun smartcastOnReceiver(x: Int?) {
if (x.isNull()) {
x<!UNSAFE_CALL!>.<!>inc()
}
else {
<!DEBUG_INFO_SMARTCAST!>x<!>.dec()
}
}
class UnstableReceiver {
var x: Int? = 42
fun smartcastOnUnstableReceiver() {
if (x.isNull()) {
x<!UNSAFE_CALL!>.<!>inc()
}
else {
<!SMARTCAST_IMPOSSIBLE!>x<!>.dec()
}
}
}