Files
Aleksandra.Arsenteva c6b32200df [Test] Add diagnostic tests for smartcast in K2
List of issues:
KT-28806, KT-7186, KT-22997, KT-1982, KT-22996, KT-54443, KT-37308, KT-37115, KT-4113, KT-25747, KT-24779
2023-12-18 13:08:33 +00:00

55 lines
939 B
Kotlin
Vendored

// ISSUE: KT-37308
// WITH_STDLIB
// DIAGNOSTICS: -ERROR_SUPPRESSION
import kotlin.contracts.*
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNotNull(): Boolean {
contract {
returns(true) implies (this@valueIsNotNull != null)
}
return this != null
}
@OptIn(ExperimentalContracts::class)
fun CharSequence?.valueIsNull(): Boolean {
contract {
returns(false) implies (this@valueIsNull != null)
}
return this == null
}
class A {
val b: String? = ""
val e: C? = C()
}
class C {
val d: String? = ""
}
fun test1(a: A?) {
if (!a?.b.valueIsNull()) {
a.b.length
}
}
fun test2(a: A?) {
require(!a?.b.valueIsNull())
a.b.length
}
fun test3(a: A?) {
if(a?.b.valueIsNotNull()){
a.b.length
}
}
fun test4(a :A?) {
require(a?.b.valueIsNotNull())
a.b.length
}
fun test5(a :A?) {
require(a?.e?.d.valueIsNotNull())
a.e.d.length
}