Files
kotlin-fork/compiler/testData/diagnostics/tests/smartCasts/equals.kt
T
Brian Norman ccda8ae23d [FIR] Comparison to String implies smart cast to String
When the left-hand side of an equality comparison is known to be a
String, and the equality condition resolves to true, then the right-hand
side can be smart-cast to a String as well. This was working for String
expressions on the left-hand side but not for String constants.

^KT-57513 Fixed
2023-10-10 11:20:40 +00:00

54 lines
1.5 KiB
Kotlin
Vendored

fun foo(x: String?) = x
class Test
class TestWithEquals {
override fun equals(other: Any?) = super.equals(other)
}
fun bar(i: Test?) {
if (i == null) foo(<!DEBUG_INFO_CONSTANT!>i<!>)
}
fun bar(i: TestWithEquals?) {
if (i == null) foo(<!DEBUG_INFO_CONSTANT!>i<!>)
if (null == i) foo(<!DEBUG_INFO_CONSTANT!>i<!>)
when (i) {
null -> foo(<!DEBUG_INFO_CONSTANT!>i<!>)
}
}
fun gav(i: TestWithEquals?, j: TestWithEquals?) {
if (j == null) {
if (i == <!DEBUG_INFO_CONSTANT!>j<!>) foo(<!DEBUG_INFO_CONSTANT!>i<!>)
}
}
fun string(foo: Any) {
val string = ""
if ("" == foo) <!DEBUG_INFO_SMARTCAST!>foo<!>.length
if (string == foo) <!DEBUG_INFO_SMARTCAST!>foo<!>.length
if (foo == "") foo.<!UNRESOLVED_REFERENCE!>length<!>
}
fun int(foo: Any) {
val int = 1
if (1 == foo) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1)
if (int == foo) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1)
if (foo == 1) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1)
}
fun long(foo: Any) {
val long = 1L
if (1L == foo) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1L)
if (long == foo) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1L)
if (foo == 1L) foo.<!UNRESOLVED_REFERENCE_WRONG_RECEIVER!>plus<!>(1L)
}
fun char(foo: Any) {
val char = 'a'
if ('a' == foo) foo.<!UNRESOLVED_REFERENCE!>compareTo<!>('a')
if (char == foo) foo.<!UNRESOLVED_REFERENCE!>compareTo<!>('a')
if (foo == 'a') foo.<!UNRESOLVED_REFERENCE!>compareTo<!>('a')
}