More precise value reassignment analysis. #KT-7418 Fixed. #KT-6902 Fixed.

Variable can now be "exactly initialized", "may be initialized" and "not initialized".
A set of relevant tests. Some fixed tests.
This commit is contained in:
Mikhail Glukhikh
2015-09-01 19:16:27 +03:00
parent c59b118b09
commit 285d5d06c7
26 changed files with 269 additions and 53 deletions
@@ -10,7 +10,7 @@ fun foo() {
}
---------------------
L0:
1 <START> INIT: in: {} out: {} USE: in: {} out: {}
1 <START> INIT: in: {} out: {} USE: in: {} out: {}
2 mark({ val b: Boolean if (1 < 2) { use(b) } else { b = true } })
v(val b: Boolean) INIT: in: {} out: {b=D}
mark(if (1 < 2) { use(b) } else { b = true }) INIT: in: {b=D} out: {b=D}
@@ -19,23 +19,23 @@ L0:
mark(1 < 2)
call(1 < 2, compareTo|<v0>, <v1>) -> <v2>
jf(L2|<v2>)
3 mark({ use(b) }) USE: in: {b=READ} out: {b=READ}
r(b) -> <v3> USE: in: {} out: {b=READ}
3 mark({ use(b) }) USE: in: {b=READ} out: {b=READ}
r(b) -> <v3> USE: in: {} out: {b=READ}
mark(use(b))
call(use(b), use|<v3>) -> <v4>
2 jmp(L3) USE: in: {} out: {}
2 jmp(L3) USE: in: {} out: {}
L2 [else branch]:
3 mark({ b = true })
r(true) -> <v5> USE: in: {b=ONLY_WRITTEN_NEVER_READ} out: {b=ONLY_WRITTEN_NEVER_READ}
w(b|<v5>) INIT: in: {b=D} out: {b=ID} USE: in: {} out: {b=ONLY_WRITTEN_NEVER_READ}
r(true) -> <v5> USE: in: {b=ONLY_WRITTEN_NEVER_READ} out: {b=ONLY_WRITTEN_NEVER_READ}
w(b|<v5>) INIT: in: {b=D} out: {b=ID} USE: in: {} out: {b=ONLY_WRITTEN_NEVER_READ}
L3 ['if' expression result]:
2 merge(if (1 < 2) { use(b) } else { b = true }|<v4>, !<v6>) -> <v7> INIT: in: {b=D} out: {b=D}
2 merge(if (1 < 2) { use(b) } else { b = true }|<v4>, !<v6>) -> <v7> INIT: in: {b=I?D} out: {b=I?D}
L1:
1 <END> INIT: in: {} out: {}
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
<SINK> USE: in: {} out: {}
=====================
== use ==
fun use(vararg a: Any?) = a
@@ -10,7 +10,7 @@ L0:
v(val sum: (Int)->Int = { (x: Int) -> sum(x - 1) + x }) INIT: in: {} out: {sum=D}
mark({ (x: Int) -> sum(x - 1) + x }) INIT: in: {sum=D} out: {sum=D}
jmp?(L2)
d({ (x: Int) -> sum(x - 1) + x }) USE: in: {sum=READ} out: {sum=READ}
d({ (x: Int) -> sum(x - 1) + x }) USE: in: {sum=READ} out: {sum=READ}
L2 [after local declaration]:
r({ (x: Int) -> sum(x - 1) + x }) -> <v0>
w(sum|<v0>) INIT: in: {sum=D} out: {sum=ID}
@@ -19,7 +19,7 @@ L1:
error:
<ERROR> INIT: in: {} out: {}
sink:
<SINK> INIT: in: {sum=D} out: {sum=D} USE: in: {} out: {}
<SINK> INIT: in: {sum=I?D} out: {sum=I?D} USE: in: {} out: {}
=====================
== anonymous_0 ==
{ (x: Int) ->
@@ -101,7 +101,7 @@ L1:
error:
<ERROR> INIT: in: {} out: {}
sink:
<SINK> INIT: in: {obj=D} out: {obj=D} USE: in: {} out: {}
<SINK> INIT: in: {obj=I?D} out: {obj=I?D} USE: in: {} out: {}
=====================
== foo ==
fun foo() {
@@ -23,9 +23,9 @@ L0:
v(i) INIT: in: {numbers=ID} out: {i=D, numbers=ID}
L2 [loop entry point]:
L6 [condition entry point]:
jmp?(L3) INIT: in: {i=D, numbers=ID} out: {i=D, numbers=ID}
jmp?(L3) INIT: in: {i=I?D, numbers=ID} out: {i=I?D, numbers=ID}
magic[LOOP_RANGE_ITERATION](numbers|<v1>) -> <v2>
w(i|<v2>) INIT: in: {i=D, numbers=ID} out: {i=ID, numbers=ID}
w(i|<v2>) INIT: in: {i=I?D, numbers=ID} out: {i=ID, numbers=ID}
mark(for (i in numbers) { val b: Boolean if (1 < 2) { b = false } else { b = true } use(b) continue }) INIT: in: {i=ID, numbers=ID} out: {i=ID, numbers=ID} USE: in: {} out: {}
L4 [body entry point]:
4 mark({ val b: Boolean if (1 < 2) { b = false } else { b = true } use(b) continue })
@@ -53,7 +53,7 @@ L8 ['if' expression result]:
- 3 jmp(L2)
L3 [loop exit point]:
L5 [body exit point]:
read (Unit) INIT: in: {i=D, numbers=ID} out: {i=D, numbers=ID}
read (Unit) INIT: in: {i=I?D, numbers=ID} out: {i=I?D, numbers=ID}
L1:
1 <END> INIT: in: {numbers=ID} out: {numbers=ID}
error:
@@ -19,9 +19,9 @@ L0:
v(i) INIT: in: {} out: {i=D}
L2 [loop entry point]:
L6 [condition entry point]:
jmp?(L3) INIT: in: {i=D} out: {i=D}
jmp?(L3) INIT: in: {i=I?D} out: {i=I?D}
magic[LOOP_RANGE_ITERATION](1..10|<v3>) -> <v4>
w(i|<v4>) INIT: in: {i=D} out: {i=ID}
w(i|<v4>) INIT: in: {i=I?D} out: {i=ID}
mark(for (i in 1..10) { val a = i }) INIT: in: {i=ID} out: {i=ID}
L4 [body entry point]:
4 mark({ val a = i })
@@ -31,7 +31,7 @@ L4 [body entry point]:
3 jmp(L2) INIT: in: {i=ID} out: {i=ID} USE: in: {i=READ} out: {i=READ}
L3 [loop exit point]:
L5 [body exit point]:
read (Unit) INIT: in: {i=D} out: {i=D}
read (Unit) INIT: in: {i=I?D} out: {i=I?D}
2 mark("after") INIT: in: {} out: {}
r("after") -> <v6>
L1:
@@ -0,0 +1,10 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(k: Int): Int {
val i: Int
for (j in 1..k) {
<!VAL_REASSIGNMENT!>i<!> = j
}
i = 6
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ k: kotlin.Int): kotlin.Int
@@ -0,0 +1,10 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(): Int {
val i: Int
var j = 0
do {
<!VAL_REASSIGNMENT!>i<!> = ++j
} while (j < 5)
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(): kotlin.Int
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(f: Boolean): Int {
val i: Int
if (f) {}
else {
i = 2
}
<!VAL_REASSIGNMENT!>i<!> = 3
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ f: kotlin.Boolean): kotlin.Int
@@ -0,0 +1,9 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(k: Int): Int {
val i: Int
for (j in 1..k) {
<!VAL_REASSIGNMENT!>i<!> = j
}
return <!UNINITIALIZED_VARIABLE!>i<!>
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ k: kotlin.Int): kotlin.Int
+10
View File
@@ -0,0 +1,10 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(f: Boolean): Int {
val i: Int
if (f) {
i = 1
}
<!VAL_REASSIGNMENT!>i<!> = 3
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ f: kotlin.Boolean): kotlin.Int
@@ -0,0 +1,13 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(f: Boolean): Int {
val i: Int
if (f) {
i = 1
}
else {
i = 2
}
<!VAL_REASSIGNMENT!>i<!> = 3
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ f: kotlin.Boolean): kotlin.Int
@@ -0,0 +1,6 @@
fun foo(f: Boolean): Int {
val i: Int
if (f) {}
i = 3
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ f: kotlin.Boolean): kotlin.Int
@@ -0,0 +1,10 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(f: Boolean): Int {
val i: Int
when (f) {
true -> i = 1
}
<!VAL_REASSIGNMENT!>i<!> = 3
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(/*0*/ f: kotlin.Boolean): kotlin.Int
@@ -0,0 +1,11 @@
// !DIAGNOSTICS: -UNUSED_VALUE
fun foo(): Int {
val i: Int
var j = 0
while (true) {
<!VAL_REASSIGNMENT!>i<!> = ++j
if (j > 5) break
}
return i
}
@@ -0,0 +1,3 @@
package
internal fun foo(): kotlin.Int