5b08c300f4
This also fixes some returnsNotNull contracts because the old code added an implication that `== true` => `!= null` then promptly removed any statement that this could've affected if the argument was a synthetic variable. ^KT-26612 tag fixed-in-k2
57 lines
1.5 KiB
Kotlin
Vendored
57 lines
1.5 KiB
Kotlin
Vendored
// !LANGUAGE: +AllowContractsForCustomFunctions +UseReturnsEffect
|
|
// !OPT_IN: kotlin.contracts.ExperimentalContracts
|
|
// !DIAGNOSTICS: -INVISIBLE_REFERENCE -INVISIBLE_MEMBER
|
|
|
|
import kotlin.contracts.*
|
|
|
|
fun safeIsString(x: Any?): Boolean? {
|
|
<!WRONG_IMPLIES_CONDITION!>contract {
|
|
returns(true) implies (x is String)
|
|
}<!>
|
|
return x?.let { it is String }
|
|
}
|
|
|
|
fun elseWithNullableResult(x: Any?) {
|
|
when (safeIsString(x)) {
|
|
false -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
else -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
|
|
when (safeIsString(x)) {
|
|
true -> x.length
|
|
else -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
|
|
when (safeIsString(x)) {
|
|
true -> x.length
|
|
false -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
else -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
|
|
when (safeIsString(x)) {
|
|
true -> x.length
|
|
null -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
else -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
}
|
|
|
|
fun exhaustiveWithNullableResult(x: Any?) {
|
|
when (safeIsString(x)) {
|
|
true -> x.length
|
|
false -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
null -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
|
|
when (safeIsString(x)) {
|
|
false -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
true -> x.length
|
|
null -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
}
|
|
|
|
when (safeIsString(x)) {
|
|
false -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
null -> x.<!UNRESOLVED_REFERENCE!>length<!>
|
|
true -> x.length
|
|
}
|
|
}
|