Control-flow analysis: do-while scope is ended after condition but before jump #KT-15334 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-01-09 18:49:17 +03:00
parent b7a5ff456f
commit b81268cca1
14 changed files with 208 additions and 25 deletions
@@ -734,6 +734,7 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
builder.bindLabel(loopInfo.conditionEntryPoint)
val condition = expression.condition
generateInstructions(condition)
builder.exitBlockScope(expression)
if (!CompileTimeConstantUtils.canBeReducedToBooleanConstant(condition, trace.bindingContext, true)) {
builder.jumpOnTrue(loopInfo.entryPoint, expression, builder.getBoundValue(expression.condition))
}
@@ -744,7 +745,6 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
}
builder.bindLabel(loopInfo.exitPoint)
builder.loadUnit(expression)
builder.exitBlockScope(expression)
}
override fun visitForExpression(expression: KtForExpression) {
@@ -0,0 +1,83 @@
== test ==
fun test() {
do {
val s: String
s = ""
} while (s == "")
}
---------------------
L0:
1 <START> INIT: in: {} out: {}
2 mark({ do { val s: String s = "" } while (s == "") }) USE: in: {} out: {}
3 mark(do { val s: String s = "" } while (s == ""))
L2 [loop entry point]:
L4 [body entry point]:
mark({ val s: String s = "" })
v(val s: String) INIT: in: {} out: {s=D}
mark("") INIT: in: {s=D} out: {s=D}
r("") -> <v0> USE: in: {s=WRITTEN_AFTER_READ} out: {s=WRITTEN_AFTER_READ}
w(s|<v0>) INIT: in: {s=D} out: {s=ID} USE: in: {s=READ} out: {s=WRITTEN_AFTER_READ}
L5 [body exit point]:
L6 [condition entry point]:
r(s) -> <v2> INIT: in: {s=ID} out: {s=ID} USE: in: {} out: {s=READ}
mark("")
r("") -> <v3>
mark(s == "")
call(s == "", equals|<v2>, <v3>) -> <v4>
2 jt(L2|<v4>) INIT: in: {} out: {}
L3 [loop exit point]:
read (Unit)
L1:
1 <END>
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
=====================
== test2 ==
fun test2() {
while (true) {
val s: String
s = ""
if (s != "") break
}
}
---------------------
L0:
1 <START> INIT: in: {} out: {}
2 mark({ while (true) { val s: String s = "" if (s != "") break } })
L2 [loop entry point]:
L6 [condition entry point]:
r(true) -> <v0>
mark(while (true) { val s: String s = "" if (s != "") break })
magic[VALUE_CONSUMER](true|<v0>) -> <v1> USE: in: {} out: {}
L4 [body entry point]:
3 mark({ val s: String s = "" if (s != "") break })
v(val s: String) INIT: in: {} out: {s=D}
mark("") INIT: in: {s=D} out: {s=D}
r("") -> <v2> USE: in: {s=WRITTEN_AFTER_READ} out: {s=WRITTEN_AFTER_READ}
w(s|<v2>) INIT: in: {s=D} out: {s=ID} USE: in: {s=READ} out: {s=WRITTEN_AFTER_READ}
mark(if (s != "") break) INIT: in: {s=ID} out: {s=ID} USE: in: {s=READ} out: {s=READ}
r(s) -> <v3> USE: in: {} out: {s=READ}
mark("")
r("") -> <v4>
mark(s != "")
call(s != "", equals|<v3>, <v4>) -> <v5>
jf(L7|<v5>)
jmp(L3) USE: in: {} out: {}
- jmp(L8)
L7 [else branch]:
read (Unit) INIT: in: {s=ID} out: {s=ID}
L8 ['if' expression result]:
merge(if (s != "") break|!<v6>) -> <v7>
2 jmp(L2) INIT: in: {} out: {}
L3 [loop exit point]:
L5 [body exit point]:
read (Unit)
L1:
1 <END>
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
=====================
@@ -0,0 +1,16 @@
// See KT-15334: incorrect reassignment in do...while
fun test() {
do {
val s: String
s = ""
} while (s == "")
}
fun test2() {
while (true) {
val s: String
s = ""
if (s != "") break
}
}
@@ -0,0 +1,38 @@
== test ==
fun test() {
do {
val s: String
s = ""
} while (s == "")
}
---------------------
"" <v0>: String NEW: r("") -> <v0>
s = "" !<v1>: *
{ val s: String s = "" } !<v1>: * COPY
s <v2>: OR{*, *} NEW: r(s) -> <v2>
"" <v3>: * NEW: r("") -> <v3>
s == "" <v4>: Boolean NEW: call(s == "", equals|<v2>, <v3>) -> <v4>
do { val s: String s = "" } while (s == "") !<v5>: *
{ do { val s: String s = "" } while (s == "") } !<v5>: * COPY
=====================
== test2 ==
fun test2() {
while (true) {
val s: String
s = ""
if (s != "") break
}
}
---------------------
<v1>: * NEW: magic[VALUE_CONSUMER](true|<v0>) -> <v1>
true <v0>: Boolean NEW: r(true) -> <v0>
"" <v2>: String NEW: r("") -> <v2>
s <v3>: OR{*, *} NEW: r(s) -> <v3>
"" <v4>: * NEW: r("") -> <v4>
s != "" <v5>: Boolean NEW: call(s != "", equals|<v3>, <v4>) -> <v5>
break !<v6>: *
if (s != "") break <v7>: * NEW: merge(if (s != "") break|!<v6>) -> <v7>
{ val s: String s = "" if (s != "") break } <v7>: * COPY
while (true) { val s: String s = "" if (s != "") break } !<v8>: *
{ while (true) { val s: String s = "" if (s != "") break } } !<v8>: * COPY
=====================
@@ -11,33 +11,33 @@ L0:
v(cond1: Boolean) INIT: in: {} out: {cond1=D}
magic[FAKE_INITIALIZER](cond1: Boolean) -> <v0> INIT: in: {cond1=D} out: {cond1=D}
w(cond1|<v0>) INIT: in: {cond1=D} out: {cond1=ID}
2 mark({ do { if (cond1) continue val cond2 = false } while (cond2) }) INIT: in: {cond1=ID} out: {cond1=ID} USE: in: {cond1=READ} out: {cond1=READ}
2 mark({ do { if (cond1) continue val cond2 = false } while (cond2) }) INIT: in: {cond1=ID} out: {cond1=ID} USE: in: {cond1=READ} out: {cond1=READ}
3 mark(do { if (cond1) continue val cond2 = false } while (cond2))
L2 [loop entry point]:
L4 [body entry point]:
mark({ if (cond1) continue val cond2 = false }) INIT: in: {cond1=ID, cond2=I?} out: {cond1=ID, cond2=I?}
mark({ if (cond1) continue val cond2 = false })
mark(if (cond1) continue)
r(cond1) -> <v1>
jf(L7|<v1>)
jmp(L6) USE: in: {cond1=READ, cond2=READ} out: {cond1=READ, cond2=READ}
jmp(L6) USE: in: {cond1=READ, cond2=READ} out: {cond1=READ, cond2=READ}
- jmp(L8)
L7 [else branch]:
read (Unit) INIT: in: {cond1=ID, cond2=I?} out: {cond1=ID, cond2=I?}
read (Unit) INIT: in: {cond1=ID} out: {cond1=ID}
L8 ['if' expression result]:
merge(if (cond1) continue|!<v2>) -> <v3>
v(val cond2 = false) INIT: in: {cond1=ID, cond2=I?} out: {cond1=ID, cond2=I?D}
r(false) -> <v4> INIT: in: {cond1=ID, cond2=I?D} out: {cond1=ID, cond2=I?D}
w(cond2|<v4>) INIT: in: {cond1=ID, cond2=I?D} out: {cond1=ID, cond2=ID}
v(val cond2 = false) INIT: in: {cond1=ID} out: {cond1=ID, cond2=D}
r(false) -> <v4> INIT: in: {cond1=ID, cond2=D} out: {cond1=ID, cond2=D}
w(cond2|<v4>) INIT: in: {cond1=ID, cond2=D} out: {cond1=ID, cond2=ID} USE: in: {cond1=READ, cond2=READ} out: {cond1=READ, cond2=READ}
L5 [body exit point]:
L6 [condition entry point]:
r(cond2) -> <v5> INIT: in: {cond1=ID, cond2=I?} out: {cond1=ID, cond2=I?}
jt(L2|<v5>) USE: in: {cond1=READ, cond2=READ} out: {cond1=READ, cond2=READ}
r(cond2) -> <v5> INIT: in: {cond1=ID, cond2=I?} out: {cond1=ID, cond2=I?} USE: in: {cond1=READ} out: {cond1=READ, cond2=READ}
2 jt(L2|<v5>) INIT: in: {cond1=ID} out: {cond1=ID} USE: in: {cond1=READ} out: {cond1=READ}
L3 [loop exit point]:
read (Unit)
L1:
1 <END> INIT: in: {cond1=ID} out: {cond1=ID}
1 <END>
error:
<ERROR> INIT: in: {} out: {}
sink:
<SINK> INIT: in: {cond1=I?} out: {cond1=I?} USE: in: {} out: {}
<SINK> INIT: in: {cond1=I?} out: {cond1=I?} USE: in: {} out: {}
=====================
@@ -11,29 +11,29 @@ L0:
1 <START> INIT: in: {} out: {}
2 mark({ "before" do { var a = 2 } while (a > 0) "after" })
mark("before")
r("before") -> <v0> USE: in: {} out: {}
r("before") -> <v0> USE: in: {} out: {}
3 mark(do { var a = 2 } while (a > 0))
L2 [loop entry point]:
L4 [body entry point]:
mark({ var a = 2 }) INIT: in: {a=I?} out: {a=I?}
v(var a = 2) INIT: in: {a=I?} out: {a=I?D}
r(2) -> <v1> INIT: in: {a=I?D} out: {a=I?D}
w(a|<v1>) INIT: in: {a=I?D} out: {a=ID}
mark({ var a = 2 })
v(var a = 2) INIT: in: {} out: {a=D}
r(2) -> <v1> INIT: in: {a=D} out: {a=D}
w(a|<v1>) INIT: in: {a=D} out: {a=ID} USE: in: {a=READ} out: {a=READ}
L5 [body exit point]:
L6 [condition entry point]:
r(a) -> <v2> INIT: in: {a=ID} out: {a=ID}
r(a) -> <v2> INIT: in: {a=ID} out: {a=ID} USE: in: {} out: {a=READ}
r(0) -> <v3>
mark(a > 0)
call(a > 0, compareTo|<v2>, <v3>) -> <v4>
jt(L2|<v4>) USE: in: {a=READ} out: {a=READ}
2 jt(L2|<v4>) INIT: in: {} out: {}
L3 [loop exit point]:
read (Unit)
2 mark("after") INIT: in: {} out: {}
mark("after")
r("after") -> <v5>
L1:
1 <END>
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
<SINK> USE: in: {} out: {}
=====================
@@ -30,11 +30,11 @@ L4 [body entry point]:
L5 [body exit point]:
L6 [condition entry point]:
r(true) -> <v0>
magic[VALUE_CONSUMER](true|<v0>) -> <v1>
2 magic[VALUE_CONSUMER](true|<v0>) -> <v1>
jmp(L2) NEXT:[mark({ })]
L3 [loop exit point]:
- read (Unit) PREV:[]
- 2 mark(unreachable()) PREV:[]
- mark(unreachable()) PREV:[]
- call(unreachable(), unreachable) -> <v2> PREV:[]
L1:
1 <END> NEXT:[<SINK>] PREV:[]
@@ -50,7 +50,7 @@ L6 [condition entry point]:
- r(1) -> <v2> PREV:[]
- mark(0 > 1) PREV:[]
- call(0 > 1, compareTo|<v1>, <v2>) -> <v3> PREV:[]
- jt(L2|<v3>) NEXT:[read (Unit), mark({return})] PREV:[]
- 2 jt(L2|<v3>) NEXT:[read (Unit), mark({return})] PREV:[]
L3 [loop exit point]:
- read (Unit) PREV:[]
L1:
@@ -29,7 +29,7 @@ L8 ['if' expression result]:
L5 [body exit point]:
L6 [condition entry point]:
r(true) -> <v5>
magic[VALUE_CONSUMER](true|<v5>) -> <v6>
2 magic[VALUE_CONSUMER](true|<v5>) -> <v6>
jmp(L2) NEXT:[mark({ if (b) break; continue; })]
L3 [loop exit point]:
read (Unit) PREV:[jmp(L3)]
@@ -0,0 +1,23 @@
// See KT-15334: incorrect reassignment in do...while
fun test() {
do {
val s: String
s = ""
} while (s == "")
}
fun test2() {
do {
val s: String
s = "1"
<!VAL_REASSIGNMENT!>s<!> = s + "2"
} while (s == "1")
}
fun test3() {
val s: String
do {
<!VAL_REASSIGNMENT!>s<!> = ""
} while (s != "")
}
@@ -0,0 +1,5 @@
package
public fun test(): kotlin.Unit
public fun test2(): kotlin.Unit
public fun test3(): kotlin.Unit
@@ -95,6 +95,12 @@ public class DataFlowTestGenerated extends AbstractDataFlowTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg-variables/bugs"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("doWhileAssignment.kt")
public void testDoWhileAssignment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/doWhileAssignment.kt");
doTest(fileName);
}
@TestMetadata("doWhileNotDefined.kt")
public void testDoWhileNotDefined() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/doWhileNotDefined.kt");
@@ -865,6 +865,12 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg-variables/bugs"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("doWhileAssignment.kt")
public void testDoWhileAssignment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/doWhileAssignment.kt");
doTest(fileName);
}
@TestMetadata("doWhileNotDefined.kt")
public void testDoWhileNotDefined() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/doWhileNotDefined.kt");
@@ -3409,6 +3409,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("doWhileAssignment.kt")
public void testDoWhileAssignment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileAssignment.kt");
doTest(fileName);
}
@TestMetadata("doWhileNotDefined.kt")
public void testDoWhileNotDefined() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/doWhileNotDefined.kt");