Files
kotlin-fork/compiler/fir/analysis-tests/testData/resolve/smartcasts/safeCalls/assignSafeCall.kt
T
2020-03-19 09:51:01 +03:00

60 lines
874 B
Kotlin
Vendored

// !DUMP_CFG
// ----------------- Stable -----------------
class A {
fun foo(): Int = 1
val x: Int = 1
fun bar() {}
}
fun test_1(a: A?) {
val x = a?.x
if (x != null) {
a.bar() // Should be OK
}
}
fun test_2(a: A?) {
val x = a?.foo()
if (x != null) {
a.bar() // Should be OK
}
}
fun test_3(x: Any?) {
val a = x as? A ?: return
a.foo() // Should be OK
x.foo() // Should be OK
}
// ----------------- Unstable -----------------
interface B {
fun foo(): Int
val x: Int
fun bar()
}
fun test_1(a: B?) {
val x = a?.x
if (x != null) {
a.bar() // Should be OK
}
}
fun test_2(a: B?) {
val x = a?.foo()
if (x != null) {
a.bar() // Should be OK
}
}
fun test_3(x: Any?) {
val a = x as? B ?: return
a.foo() // Should be OK
x.foo() // Should be OK
}