Smart casts to nothing after comparison with null

This commit is contained in:
Mikhail Glukhikh
2015-11-05 17:01:28 +03:00
committed by Mikhail Glukhikh
parent 981d471ebe
commit b556037915
7 changed files with 26 additions and 11 deletions
@@ -219,11 +219,11 @@ import static org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability.NOT_NULL
newTypeInfo.putAll(a, collectTypesFromMeAndParents(b));
newTypeInfo.putAll(b, collectTypesFromMeAndParents(a));
if (!a.getType().equals(b.getType())) {
// To avoid smart casts to Nothing or Nothing? and recording base types of own type
if (!KotlinBuiltIns.isNothingOrNullableNothing(b.getType()) && !TypeUtilsKt.isSubtypeOf(a.getType(), b.getType())) {
// To avoid recording base types of own type
if (!TypeUtilsKt.isSubtypeOf(a.getType(), b.getType())) {
newTypeInfo.put(a, b.getType());
}
if (!KotlinBuiltIns.isNothingOrNullableNothing(a.getType()) && !TypeUtilsKt.isSubtypeOf(b.getType(), a.getType())) {
if (!TypeUtilsKt.isSubtypeOf(b.getType(), a.getType())) {
newTypeInfo.put(b, a.getType());
}
}
@@ -3,19 +3,19 @@ fun bar(x: Int) = x + 1
fun f1(x: Int?) {
bar(<!TYPE_MISMATCH!>x<!>)
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) bar(x!!)
if (x == null) <!UNREACHABLE_CODE!>bar(<!><!DEBUG_INFO_SMARTCAST!>x<!>!!<!UNREACHABLE_CODE!>)<!>
}
fun f2(x: Int?) {
if (x != null) else bar(x!!)
if (x != null) else <!DEBUG_INFO_SMARTCAST!>x<!>!!
}
fun f3(x: Int?) {
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else bar(x!!)
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else <!DEBUG_INFO_SMARTCAST!>x<!>!!
}
fun f4(x: Int?) {
if (x == null) bar(x!!) else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) <!DEBUG_INFO_SMARTCAST!>x<!>!! else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
fun f5(x: Int?) {
@@ -22,8 +22,8 @@ fun main(args : Array<String>) {
foo(<!DEBUG_INFO_SMARTCAST!>x<!>)
} else {
foo(<!TYPE_MISMATCH!>x<!>)
foo(x!!)
foo(<!DEBUG_INFO_SMARTCAST!>x<!>)
<!UNREACHABLE_CODE!>foo(<!><!DEBUG_INFO_SMARTCAST!>x<!>!!<!UNREACHABLE_CODE!>)<!>
<!UNREACHABLE_CODE!>foo(<!DEBUG_INFO_SMARTCAST!>x<!>)<!>
}
foo(<!DEBUG_INFO_SMARTCAST!>x<!>)
@@ -2,13 +2,13 @@ fun <T> test(t: T): T {
if (t != null) {
return t<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
}
return t!!
return <!DEBUG_INFO_SMARTCAST!>t<!>!!
}
fun <T> T.testThis(): String {
if (this != null) {
return this<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.toString()
}
return this!!.toString()
return <!DEBUG_INFO_SMARTCAST!>this<!>!!.toString()
}
@@ -0,0 +1,6 @@
fun bar(x: Int?): Int {
if (x != null) return -1
if (<!SENSELESS_COMPARISON!>x == null<!>) return -2
// Should be unreachable
return 2 + 2
}
@@ -0,0 +1,3 @@
package
public fun bar(/*0*/ x: kotlin.Int?): kotlin.Int
@@ -14625,6 +14625,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("notNullorNotNull.kt")
public void testNotNullorNotNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/notNullorNotNull.kt");
doTest(fileName);
}
@TestMetadata("ownerDeclaresBothModifies.kt")
public void testOwnerDeclaresBothModifies() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/smartCasts/ownerDeclaresBothModifies.kt");