[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
This commit is contained in:
@@ -8,8 +8,8 @@ import checkSubtype
|
||||
fun main(args : Array<String>) {
|
||||
val x = checkSubtype<Any>(args[0])
|
||||
if(x is <!PLATFORM_CLASS_MAPPED_TO_KOTLIN!>java.lang.CharSequence<!>) {
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!>) x.<!FUNCTION_CALL_EXPECTED!>length<!> else x.length() // OK
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> || <!EQUALITY_NOT_APPLICABLE_WARNING!>"b" == x<!>) x.<!FUNCTION_CALL_EXPECTED!>length<!> else x.length() // <– THEN ERROR
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> && <!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!>) x.<!FUNCTION_CALL_EXPECTED!>length<!> else x.length() // <– ELSE ERROR
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!>) x.length else x.length() // OK
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> || <!EQUALITY_NOT_APPLICABLE_WARNING!>"b" == x<!>) x.length else x.length() // <– THEN ERROR
|
||||
if (<!EQUALITY_NOT_APPLICABLE_WARNING!>"a" == x<!> && "a" == x) x.length else x.length() // <– ELSE ERROR
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,3 +23,31 @@ fun gav(i: TestWithEquals?, j: TestWithEquals?) {
|
||||
if (i == j) foo(i)
|
||||
}
|
||||
}
|
||||
|
||||
fun string(foo: Any) {
|
||||
val string = ""
|
||||
if ("" == foo) foo.length
|
||||
if (string == foo) foo.length
|
||||
if (foo == "") foo.<!UNRESOLVED_REFERENCE!>length<!>
|
||||
}
|
||||
|
||||
fun int(foo: Any) {
|
||||
val int = 1
|
||||
if (1 == foo) foo.<!UNRESOLVED_REFERENCE!>plus<!>(1)
|
||||
if (int == foo) foo.<!UNRESOLVED_REFERENCE!>plus<!>(1)
|
||||
if (foo == 1) foo.<!UNRESOLVED_REFERENCE!>plus<!>(1)
|
||||
}
|
||||
|
||||
fun long(foo: Any) {
|
||||
val long = 1L
|
||||
if (1L == foo) foo.<!UNRESOLVED_REFERENCE!>plus<!>(1L)
|
||||
if (long == foo) foo.<!UNRESOLVED_REFERENCE!>plus<!>(1L)
|
||||
if (foo == 1L) foo.<!UNRESOLVED_REFERENCE!>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')
|
||||
}
|
||||
|
||||
+29
-1
@@ -22,4 +22,32 @@ 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')
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@ package
|
||||
|
||||
public fun bar(/*0*/ i: Test?): kotlin.Unit
|
||||
public fun bar(/*0*/ i: TestWithEquals?): kotlin.Unit
|
||||
public fun char(/*0*/ foo: kotlin.Any): kotlin.Unit
|
||||
public fun foo(/*0*/ x: kotlin.String?): kotlin.String?
|
||||
public fun gav(/*0*/ i: TestWithEquals?, /*1*/ j: TestWithEquals?): kotlin.Unit
|
||||
public fun int(/*0*/ foo: kotlin.Any): kotlin.Unit
|
||||
public fun long(/*0*/ foo: kotlin.Any): kotlin.Unit
|
||||
public fun string(/*0*/ foo: kotlin.Any): kotlin.Unit
|
||||
|
||||
public final class Test {
|
||||
public constructor Test()
|
||||
|
||||
Reference in New Issue
Block a user