No smart casts to Nothing?

This commit is contained in:
Mikhail Glukhikh
2015-11-06 18:53:32 +03:00
parent 2d9fbf5696
commit b468d5b0c6
5 changed files with 10 additions and 8 deletions
@@ -22,6 +22,7 @@ import kotlin.CollectionsKt;
import kotlin.jvm.functions.Function1; import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.psi.KtExpression; import org.jetbrains.kotlin.psi.KtExpression;
import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.BindingContext;
@@ -153,6 +154,7 @@ public class SmartCastManager {
@NotNull DataFlowValue dataFlowValue, @NotNull DataFlowValue dataFlowValue,
boolean recordExpressionType boolean recordExpressionType
) { ) {
if (KotlinBuiltIns.isNullableNothing(type)) return;
if (dataFlowValue.isPredictable()) { if (dataFlowValue.isPredictable()) {
trace.record(SMARTCAST, expression, type); trace.record(SMARTCAST, expression, type);
if (recordExpressionType) { if (recordExpressionType) {
@@ -3,19 +3,19 @@ fun bar(x: Int) = x + 1
fun f1(x: Int?) { fun f1(x: Int?) {
bar(<!TYPE_MISMATCH!>x<!>) bar(<!TYPE_MISMATCH!>x<!>)
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (x == null) <!UNREACHABLE_CODE!>bar(<!><!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>x<!>!!<!UNREACHABLE_CODE!>)<!> if (x == null) <!UNREACHABLE_CODE!>bar(<!><!ALWAYS_NULL!>x<!>!!<!UNREACHABLE_CODE!>)<!>
} }
fun f2(x: Int?) { fun f2(x: Int?) {
if (x != null) else <!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>x<!>!! if (x != null) else <!ALWAYS_NULL!>x<!>!!
} }
fun f3(x: Int?) { fun f3(x: Int?) {
if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else <!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>x<!>!! if (x != null) bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) else <!ALWAYS_NULL!>x<!>!!
} }
fun f4(x: Int?) { fun f4(x: Int?) {
if (x == null) <!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>x<!>!! else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>) if (x == null) <!ALWAYS_NULL!>x<!>!! else bar(x<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
} }
fun f5(x: Int?) { fun f5(x: Int?) {
@@ -22,7 +22,7 @@ fun main(args : Array<String>) {
foo(<!DEBUG_INFO_SMARTCAST!>x<!>) foo(<!DEBUG_INFO_SMARTCAST!>x<!>)
} else { } else {
foo(<!ALWAYS_NULL, TYPE_MISMATCH!>x<!>) foo(<!ALWAYS_NULL, TYPE_MISMATCH!>x<!>)
<!UNREACHABLE_CODE!>foo(<!><!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>x<!>!!<!UNREACHABLE_CODE!>)<!> <!UNREACHABLE_CODE!>foo(<!><!ALWAYS_NULL!>x<!>!!<!UNREACHABLE_CODE!>)<!>
<!UNREACHABLE_CODE!>foo(<!DEBUG_INFO_SMARTCAST!>x<!>)<!> <!UNREACHABLE_CODE!>foo(<!DEBUG_INFO_SMARTCAST!>x<!>)<!>
} }
@@ -2,13 +2,13 @@ fun <T> test(t: T): T {
if (t != null) { if (t != null) {
return t<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!> return t<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
} }
return <!ALWAYS_NULL, DEBUG_INFO_SMARTCAST!>t<!>!! return <!ALWAYS_NULL!>t<!>!!
} }
fun <T> T.testThis(): String { fun <T> T.testThis(): String {
if (this != null) { if (this != null) {
return this<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.toString() return this<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.toString()
} }
return <!DEBUG_INFO_SMARTCAST!>this<!>!!.toString() return this!!.toString()
} }
@@ -6,6 +6,6 @@ fun foo(): String {
var t: String? = "y" var t: String? = "y"
if (t == null) t = "x" if (t == null) t = "x"
var x: Int? = null var x: Int? = null
if (x == null) <!TYPE_MISMATCH!><!DEBUG_INFO_SMARTCAST!>x<!> += null<!> if (x == null) <!TYPE_MISMATCH!>x += null<!>
return <!DEBUG_INFO_SMARTCAST!>t<!> + s return <!DEBUG_INFO_SMARTCAST!>t<!> + s
} }