assignSafeCall.kt 931 字节
Newer Older
1
// !DUMP_CFG
2
// ----------------- Stable -----------------
3

4 5
class A {
    fun foo(): Int = 1
6

7 8 9
    val x: Int = 1

    fun bar() {}
10 11 12 13 14
}

fun test_1(a: A?) {
    val x = a?.x
    if (x != null) {
15
        a.bar() // Should be OK
16 17 18 19 20 21
    }
}

fun test_2(a: A?) {
    val x = a?.foo()
    if (x != null) {
22
        a.bar() // Should be OK
23 24 25
    }
}

26
<!CONFLICTING_OVERLOADS!>fun test_3(x: Any?)<!> {
27
    val a = x as? A ?: return
28 29
    a.foo() // Should be OK
    x.foo() // Should be OK
30
}
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

// ----------------- 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
    }
}

56
<!CONFLICTING_OVERLOADS!>fun test_3(x: Any?)<!> {
57 58 59
    val a = x as? B ?: return
    a.foo() // Should be OK
    x.foo() // Should be OK
60
}