Properly check whether nullability of receiver argument fits to parameter's nullability

It's should behave like we check isSubtype(receiverType, parameterType) + use nullability info from smart cast

 #KT-1090 Fixed
This commit is contained in:
Denis Zharkov
2015-08-26 11:29:18 +03:00
parent 70d4a7997e
commit aad977d204
13 changed files with 267 additions and 10 deletions
@@ -0,0 +1,45 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
fun <T : CharSequence?> T.bar1() {}
fun CharSequence?.bar2() {}
fun <T : CharSequence> T.bar3() {}
fun CharSequence.bar4() {}
fun <T : CharSequence?> foo(x: T) {
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
x<!UNNECESSARY_SAFE_CALL!>?.<!>length()
x.bar1()
x.bar2()
x.<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
<!DEBUG_INFO_SMARTCAST!>x<!>.bar4()
x<!UNNECESSARY_SAFE_CALL!>?.<!>bar1()
}
x<!UNSAFE_CALL!>.<!>length()
if (x is String) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
<!DEBUG_INFO_SMARTCAST!>x<!><!UNNECESSARY_SAFE_CALL!>?.<!>length()
<!DEBUG_INFO_SMARTCAST!>x<!>.bar1()
x.bar2()
<!DEBUG_INFO_SMARTCAST!>x<!>.bar3()
}
if (x is CharSequence) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
x<!UNNECESSARY_SAFE_CALL!>?.<!>length()
<!DEBUG_INFO_SMARTCAST!>x<!>.bar1()
x.bar2()
<!DEBUG_INFO_SMARTCAST!>x<!>.bar3()
}
}
@@ -0,0 +1,7 @@
package
internal fun </*0*/ T : kotlin.CharSequence?> foo(/*0*/ x: T): kotlin.Unit
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
internal fun kotlin.CharSequence.bar4(): kotlin.Unit
@@ -0,0 +1,35 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
fun <T : CharSequence?> T.bar1() {}
fun CharSequence?.bar2() {}
fun <T : CharSequence> T.bar3() {}
fun CharSequence.bar4() {}
fun <T : String?> T.foo() {
if (this != null) {
if (<!SENSELESS_COMPARISON!>this != null<!>) {}
length()
this<!UNNECESSARY_SAFE_CALL!>?.<!>length()
bar1()
bar2()
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
bar4()
this<!UNNECESSARY_SAFE_CALL!>?.<!>bar1()
}
<!UNSAFE_CALL!>length<!>()
if (this is String) {
length()
this<!UNNECESSARY_SAFE_CALL!>?.<!>length()
bar1()
bar2()
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
}
}
@@ -0,0 +1,7 @@
package
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
internal fun kotlin.CharSequence.bar4(): kotlin.Unit
internal fun </*0*/ T : kotlin.String?> T.foo(): kotlin.Unit
@@ -0,0 +1,40 @@
// !DIAGNOSTICS: -UNUSED_VALUE,-UNUSED_VARIABLE,-ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE,-BASE_WITH_NULLABLE_UPPER_BOUND,-VARIABLE_WITH_REDUNDANT_INITIALIZER
class A<T : CharSequence?, E1 : T, E2: T?> {
fun T.bar() {}
fun foo(x: E1, y: E2) {
x.bar()
if (1 == 1) {
y<!UNSAFE_CALL!>.<!>bar()
}
x?.bar()
y?.bar()
var t: T = x
var tN: T? = y
// condition needed to make smart cast on tN impossible
if (1 == 1) {
tN = x
}
if (1 == 1) {
t = <!TYPE_MISMATCH!>tN<!>
}
t = <!TYPE_MISMATCH!>y<!>
// Could be smart-cast
if (y != null) {
t = <!TYPE_MISMATCH!>y<!>
}
if (tN != null) {
t = <!DEBUG_INFO_SMARTCAST!>tN<!>
}
}
}
@@ -0,0 +1,10 @@
package
internal final class A</*0*/ T : kotlin.CharSequence?, /*1*/ E1 : T, /*2*/ E2 : T?> {
public constructor A</*0*/ T : kotlin.CharSequence?, /*1*/ E1 : T, /*2*/ E2 : T?>()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
internal final fun foo(/*0*/ x: E1, /*1*/ y: E2): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
internal final fun T.bar(): kotlin.Unit
}
@@ -0,0 +1,28 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
fun <T : CharSequence?> T.bar1() {}
fun CharSequence?.bar2() {}
fun <T : CharSequence> T.bar3() {}
fun <T, R> T.let(f: (T) -> R): R = f(this)
fun <T : String?> foo(x: T) {
x<!UNSAFE_CALL!>.<!>length()
x?.length()
if (1 == 1) {
x!!.length()
}
x.bar1()
x.bar2()
x?.bar1()
x?.bar2()
x.<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
x?.let { it<!UNSAFE_CALL!>.<!>length() }
}
@@ -0,0 +1,7 @@
package
internal fun </*0*/ T : kotlin.String?> foo(/*0*/ x: T): kotlin.Unit
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
internal fun </*0*/ T, /*1*/ R> T.let(/*0*/ f: (T) -> R): R