do...while (true) is now considered infinite loop in CFA #KT-3896 Fixed

Also #KT-3883 Fixed
Also #KT-4986 Fixed
This commit is contained in:
Mikhail Glukhikh
2016-03-11 19:27:08 +03:00
parent 4cf3ec3df2
commit 569a5888ff
11 changed files with 221 additions and 10 deletions
@@ -716,8 +716,16 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
generateInstructions(expression.body)
builder.exitLoopBody(expression)
builder.bindLabel(loopInfo.conditionEntryPoint)
generateInstructions(expression.condition)
builder.jumpOnTrue(loopInfo.entryPoint, expression, builder.getBoundValue(expression.condition))
val condition = expression.condition
generateInstructions(condition)
if (!CompileTimeConstantUtils.canBeReducedToBooleanConstant(condition, trace.bindingContext, true)) {
builder.jumpOnTrue(loopInfo.entryPoint, expression, builder.getBoundValue(expression.condition))
}
else {
assert(condition != null) { "Invalid do / while condition: " + expression.text }
createSyntheticValue(condition!!, MagicKind.VALUE_CONSUMER, condition)
builder.jump(loopInfo.entryPoint, expression)
}
builder.bindLabel(loopInfo.exitPoint)
builder.loadUnit(expression)
builder.exitLexicalScope(expression)
@@ -0,0 +1,76 @@
== unreachable ==
fun unreachable() {}
---------------------
L0:
1 <START>
2 mark({})
read (Unit)
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== a ==
fun a() {
do {
} while (true)
unreachable()
}
---------------------
L0:
1 <START>
2 mark({ do { } while (true) unreachable() })
3 mark(do { } while (true))
L2 [loop entry point]:
L4 [body entry point]:
mark({ }) PREV:[mark(do { } while (true)), jmp(L2)]
read (Unit)
L5 [body exit point]:
L6 [condition entry point]:
r(true) -> <v0>
magic[VALUE_CONSUMER](true|<v0>) -> <v1>
jmp(L2) NEXT:[mark({ })]
L3 [loop exit point]:
- read (Unit) PREV:[]
- 2 mark(unreachable()) PREV:[]
- call(unreachable(), unreachable) -> <v2> PREV:[]
L1:
1 <END> NEXT:[<SINK>] PREV:[]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
== b ==
fun b() {
while (true) {
}
unreachable()
}
---------------------
L0:
1 <START>
2 mark({ while (true) { } unreachable() })
L2 [loop entry point]:
L6 [condition entry point]:
r(true) -> <v0> PREV:[mark({ while (true) { } unreachable() }), jmp(L2)]
mark(while (true) { })
magic[VALUE_CONSUMER](true|<v0>) -> <v1>
L4 [body entry point]:
3 mark({ })
read (Unit)
2 jmp(L2) NEXT:[r(true) -> <v0>]
L3 [loop exit point]:
L5 [body exit point]:
- read (Unit) PREV:[]
- mark(unreachable()) PREV:[]
- call(unreachable(), unreachable) -> <v2> PREV:[]
L1:
1 <END> NEXT:[<SINK>] PREV:[]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,13 @@
fun unreachable() {}
fun a() {
do {
} while (true)
unreachable()
}
fun b() {
while (true) {
}
unreachable()
}
@@ -0,0 +1,28 @@
== unreachable ==
fun unreachable() {}
---------------------
=====================
== a ==
fun a() {
do {
} while (true)
unreachable()
}
---------------------
<v1>: * NEW: magic[VALUE_CONSUMER](true|<v0>) -> <v1>
true <v0>: * NEW: r(true) -> <v0>
unreachable() <v2>: * NEW: call(unreachable(), unreachable) -> <v2>
{ do { } while (true) unreachable() } <v2>: * COPY
=====================
== b ==
fun b() {
while (true) {
}
unreachable()
}
---------------------
<v1>: * NEW: magic[VALUE_CONSUMER](true|<v0>) -> <v1>
true <v0>: Boolean NEW: r(true) -> <v0>
unreachable() <v2>: * NEW: call(unreachable(), unreachable) -> <v2>
{ while (true) { } unreachable() } <v2>: * COPY
=====================
@@ -15,7 +15,7 @@ L0:
3 mark(do { if (b) break; continue; } while (true))
L2 [loop entry point]:
L4 [body entry point]:
mark({ if (b) break; continue; }) PREV:[mark(do { if (b) break; continue; } while (true)), jt(L2|<v5>)]
mark({ if (b) break; continue; }) PREV:[mark(do { if (b) break; continue; } while (true)), jmp(L2)]
mark(if (b) break)
r(b) -> <v1>
jf(L7|<v1>) NEXT:[read (Unit), jmp(L3)]
@@ -29,13 +29,14 @@ L8 ['if' expression result]:
L5 [body exit point]:
L6 [condition entry point]:
r(true) -> <v5>
jt(L2|<v5>) NEXT:[read (Unit), mark({ if (b) break; continue; })]
magic[VALUE_CONSUMER](true|<v5>) -> <v6>
jmp(L2) NEXT:[mark({ if (b) break; continue; })]
L3 [loop exit point]:
read (Unit) PREV:[jmp(L3), jt(L2|<v5>)]
read (Unit) PREV:[jmp(L3)]
L1:
1 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
=====================
@@ -7,12 +7,13 @@ fun test(b: Boolean) {
}
---------------------
<v0>: Boolean NEW: magic[FAKE_INITIALIZER](b: Boolean) -> <v0>
<v6>: * NEW: magic[VALUE_CONSUMER](true|<v5>) -> <v6>
b <v1>: Boolean NEW: r(b) -> <v1>
break !<v2>: *
if (b) break <v3>: * NEW: merge(if (b) break|!<v2>) -> <v3>
continue !<v4>: *
{ if (b) break; continue; } !<v4>: * COPY
true <v5>: Boolean NEW: r(true) -> <v5>
do { if (b) break; continue; } while (true) !<v6>: *
{ do { if (b) break; continue; } while (true); } !<v6>: * COPY
=====================
true <v5>: * NEW: r(true) -> <v5>
do { if (b) break; continue; } while (true) !<v7>: *
{ do { if (b) break; continue; } while (true); } !<v7>: * COPY
=====================
@@ -0,0 +1,55 @@
fun unreachable() {}
fun a() {
do {
} while (true)
<!UNREACHABLE_CODE!>unreachable()<!>
}
fun b() {
while (true) {
}
<!UNREACHABLE_CODE!>unreachable()<!>
}
fun c() {
do {} while (1 == 1)
}
fun d() {
while (2 == 2) {}
}
fun use(arg: Any) = arg
fun f(cond: Boolean) {
val bar: Any
do {
if (cond) {
bar = "value"
break
}
} while (true)
use(bar) // should work
val foo: Any
while (true) {
if (cond) {
foo = "value"
break
}
}
use(foo) // should work
}
fun g(): Int {
do {
if (true) return 12
} while (true)
} // should work
fun h(): Int {
while (true) {
if (true) return 12
}
} // should work
@@ -0,0 +1,11 @@
package
public fun a(): kotlin.Unit
public fun b(): kotlin.Unit
public fun c(): kotlin.Unit
public fun d(): kotlin.Unit
public fun f(/*0*/ cond: kotlin.Boolean): kotlin.Unit
public fun g(): kotlin.Int
public fun h(): kotlin.Int
public fun unreachable(): kotlin.Unit
public fun use(/*0*/ arg: kotlin.Any): kotlin.Any
@@ -208,6 +208,12 @@ public class ControlFlowTestGenerated extends AbstractControlFlowTest {
doTest(fileName);
}
@TestMetadata("InfiniteLoops.kt")
public void testInfiniteLoops() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg/controlStructures/InfiniteLoops.kt");
doTest(fileName);
}
@TestMetadata("localAndNonlocalReturnsWithFinally.kt")
public void testLocalAndNonlocalReturnsWithFinally() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg/controlStructures/localAndNonlocalReturnsWithFinally.kt");
@@ -210,6 +210,12 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest {
doTest(fileName);
}
@TestMetadata("InfiniteLoops.kt")
public void testInfiniteLoops() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg/controlStructures/InfiniteLoops.kt");
doTest(fileName);
}
@TestMetadata("localAndNonlocalReturnsWithFinally.kt")
public void testLocalAndNonlocalReturnsWithFinally() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg/controlStructures/localAndNonlocalReturnsWithFinally.kt");
@@ -2877,6 +2877,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("infiniteLoops.kt")
public void testInfiniteLoops() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/infiniteLoops.kt");
doTest(fileName);
}
@TestMetadata("initializationInLambda.kt")
public void testInitializationInLambda() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/initializationInLambda.kt");