Support empty if-statements

Type-check "if (...) ;" to Unit, report "implicit cast to Unit", propagate data
flow info out of its condition

 #KT-2478 Fixed
This commit is contained in:
Alexander Udalov
2013-12-02 20:30:55 +04:00
parent f045a06dee
commit 4cd4026174
7 changed files with 72 additions and 8 deletions
@@ -0,0 +1,12 @@
fun box(): String {
if (true);
if (false);
val iftrue = if (true);
val iffalse = if (false);
var state = 0
val k = if (state++==1);
if (state != 1) return "Fail: $state"
return "OK"
}
@@ -0,0 +1,16 @@
fun foo(x: Unit) = x
fun test() {
if (false);
if (true);
val x = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (false)<!>;
foo(x)
val y: Unit = if (false);
foo(y)
foo({if (1==1);}())
return if (true);
}
@@ -0,0 +1,22 @@
fun f1(s: String?) {
if (s!! == "");
s : String
}
fun f2(s: Number?) {
if (s is Int);
<!TYPE_MISMATCH!>s<!> : Int
if (s as Int == 42);
s : Int
}
fun f3(s: Number?) {
if (s is Int && s as Int == 42);
<!TYPE_MISMATCH!>s<!> : Int
}
fun f4(s: Int?) {
var u = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (s!! == 42)<!>;
if (u == Unit.VALUE) u = if (s == 239);
return u
}