[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
This commit is contained in:
committed by
Space Team
parent
48072c822b
commit
c6b32200df
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
// 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<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
}
|
||||
|
||||
fun test2(a: A?) {
|
||||
require(!a?.b.valueIsNull())
|
||||
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
|
||||
fun test3(a: A?) {
|
||||
if(a?.b.valueIsNotNull()){
|
||||
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
}
|
||||
|
||||
fun test4(a :A?) {
|
||||
require(a?.b.valueIsNotNull())
|
||||
a<!UNSAFE_CALL!>.<!>b<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
|
||||
fun test5(a :A?) {
|
||||
require(a?.e?.d.valueIsNotNull())
|
||||
a<!UNSAFE_CALL!>.<!>e<!UNSAFE_CALL!>.<!>d<!UNSAFE_CALL!>.<!>length
|
||||
}
|
||||
Reference in New Issue
Block a user