More accurate CFG for local class constructors (now they are not considered unconditionally executed) #KT-10042 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-03-21 17:35:53 +03:00
parent 8756cba2f5
commit b975b7d26e
15 changed files with 155 additions and 36 deletions
@@ -915,7 +915,14 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
mark(expression)
val statements = expression.statements
for (statement in statements) {
val afterClassLabel = (statement as? KtClassOrObject)?.let { builder.createUnboundLabel("after local class") }
if (afterClassLabel != null) {
builder.nondeterministicJump(afterClassLabel, statement, null)
}
generateInstructions(statement)
if (afterClassLabel != null) {
builder.bindLabel(afterClassLabel)
}
}
if (statements.isEmpty()) {
builder.loadUnit(expression)
@@ -0,0 +1,31 @@
== foo ==
fun foo() {
var x: String
class A {
init {
x = ""
}
}
x.length
}
---------------------
L0:
1 <START> INIT: in: {} out: {} USE: in: {length=READ} out: {length=READ}
2 mark({ var x: String class A { init { x = "" } } x.length })
v(var x: String) INIT: in: {} out: {x=D}
jmp?(L2) INIT: in: {x=D} out: {x=D} USE: in: {length=READ, x=READ} out: {length=READ, x=READ}
3 mark({ x = "" })
mark("")
r("") -> <v0> USE: in: {length=READ, x=WRITTEN_AFTER_READ} out: {length=READ, x=WRITTEN_AFTER_READ}
w(x|<v0>) INIT: in: {x=D} out: {x=ID} USE: in: {length=READ, x=READ} out: {length=READ, x=WRITTEN_AFTER_READ}
L2 [after local class]:
2 mark(x.length) INIT: in: {x=I?D} out: {x=I?D} USE: in: {length=READ, x=READ} out: {length=READ, x=READ}
r(x) -> <v2> USE: in: {length=READ} out: {length=READ, x=READ}
r(length|<v2>) -> <v3> USE: in: {} out: {length=READ}
L1:
1 <END> INIT: in: {} out: {}
error:
<ERROR>
sink:
<SINK> USE: in: {} out: {}
=====================
@@ -0,0 +1,9 @@
fun foo() {
var x: String
class A {
init {
x = ""
}
}
x.length
}
@@ -0,0 +1,19 @@
== foo ==
fun foo() {
var x: String
class A {
init {
x = ""
}
}
x.length
}
---------------------
"" <v0>: String NEW: r("") -> <v0>
x = "" !<v1>: *
{ x = "" } !<v1>: * COPY
x <v2>: {<: CharSequence} NEW: r(x) -> <v2>
length <v3>: * NEW: r(length|<v2>) -> <v3>
x.length <v3>: * COPY
{ var x: String class A { init { x = "" } } x.length } <v3>: * COPY
=====================
@@ -17,6 +17,7 @@ L0:
2 mark({ "before" class A(val x: Int) { init { val a = x } fun foo() { val b = x } } "after" })
mark("before")
r("before") -> <v0>
jmp?(L2)
v(val x: Int) INIT: in: {} out: {x=D}
magic[FAKE_INITIALIZER](val x: Int) -> <v1> INIT: in: {x=D} out: {x=D}
w(x|<v1>) INIT: in: {x=D} out: {x=ID}
@@ -25,9 +26,10 @@ L0:
magic[IMPLICIT_RECEIVER](x) -> <v2> INIT: in: {a=D, x=ID} out: {a=D, x=ID}
r(x|<v2>) -> <v3>
w(a|<v3>) INIT: in: {a=D, x=ID} out: {a=ID, x=ID}
2 jmp?(L2) INIT: in: {x=ID} out: {x=ID}
2 jmp?(L3) INIT: in: {x=ID} out: {x=ID}
d(fun foo() { val b = x }) USE: in: {x=READ} out: {x=READ}
L2 [after local declaration]:
L2 [after local class]:
L3 [after local declaration]:
mark("after")
r("after") -> <v4>
L1:
@@ -42,14 +44,14 @@ fun foo() {
val b = x
}
---------------------
L3:
L4:
3 <START> INIT: in: {x=ID} out: {x=ID}
4 mark({ val b = x })
v(val b = x) INIT: in: {x=ID} out: {b=D, x=ID}
magic[IMPLICIT_RECEIVER](x) -> <v0> INIT: in: {b=D, x=ID} out: {b=D, x=ID} USE: in: {x=READ} out: {x=READ}
r(x|<v0>) -> <v1> USE: in: {} out: {x=READ}
w(b|<v1>) INIT: in: {b=D, x=ID} out: {b=ID, x=ID}
L4:
L5:
3 <END> INIT: in: {x=ID} out: {x=ID}
error:
<ERROR> INIT: in: {} out: {}
@@ -17,13 +17,15 @@ L0:
2 mark({ "before" object A { init { val a = 1 } fun foo() { val b = 2 } } "after" })
mark("before")
r("before") -> <v0>
jmp?(L2)
3 mark({ val a = 1 })
v(val a = 1) INIT: in: {} out: {a=D}
r(1) -> <v1> INIT: in: {a=D} out: {a=D}
w(a|<v1>) INIT: in: {a=D} out: {a=ID}
2 jmp?(L2) INIT: in: {} out: {}
2 jmp?(L3) INIT: in: {} out: {}
d(fun foo() { val b = 2 })
L2 [after local declaration]:
L2 [after local class]:
L3 [after local declaration]:
mark("after")
r("after") -> <v2>
L1:
@@ -38,13 +40,13 @@ fun foo() {
val b = 2
}
---------------------
L3:
L4:
3 <START> INIT: in: {} out: {}
4 mark({ val b = 2 })
v(val b = 2) INIT: in: {} out: {b=D}
r(2) -> <v0> INIT: in: {b=D} out: {b=D}
w(b|<v0>) INIT: in: {b=D} out: {b=ID}
L4:
L5:
3 <END> INIT: in: {} out: {}
error:
<ERROR>
@@ -14,14 +14,16 @@ fun foo() {
L0:
1 <START> INIT: in: {} out: {}
2 mark({ class A { var a : Int get() { return field } set(v: Int) { field = v } } })
jmp?(L2)
v(var a : Int get() { return field } set(v: Int) { field = v }) INIT: in: {} out: {a=D}
jmp?(L2) INIT: in: {a=D} out: {a=D} USE: in: {field=ONLY_WRITTEN_NEVER_READ, field=READ} out: {field=ONLY_WRITTEN_NEVER_READ, field=READ}
jmp?(L3) INIT: in: {a=D} out: {a=D} USE: in: {field=ONLY_WRITTEN_NEVER_READ, field=READ} out: {field=ONLY_WRITTEN_NEVER_READ, field=READ}
d(get() { return field }) USE: in: {field=READ} out: {field=READ}
L2 [after local declaration]:
jmp?(L5)
L3 [after local declaration]:
jmp?(L6)
d(set(v: Int) { field = v }) INIT: in: {a=D, field=I} out: {a=D, field=I} USE: in: {field=ONLY_WRITTEN_NEVER_READ} out: {field=ONLY_WRITTEN_NEVER_READ}
L1:
L5 [after local declaration]:
L2 [after local class]:
L6 [after local declaration]:
1 <END> INIT: in: {} out: {}
error:
<ERROR>
@@ -33,12 +35,12 @@ get() {
return field
}
---------------------
L3:
L4:
3 <START> INIT: in: {a=D} out: {a=D}
4 mark({ return field }) USE: in: {field=READ} out: {field=READ}
r(field) -> <v0> USE: in: {} out: {field=READ}
ret(*|<v0>) L4
L4:
ret(*|<v0>) L5
L5:
3 <END>
error:
<ERROR> INIT: in: {} out: {}
@@ -50,7 +52,7 @@ set(v: Int) {
field = v
}
---------------------
L6:
L7:
3 <START> INIT: in: {a=D} out: {a=D}
v(v: Int) INIT: in: {a=D} out: {a=D, v=D}
magic[FAKE_INITIALIZER](v: Int) -> <v0> INIT: in: {a=D, v=D} out: {a=D, v=D}
@@ -58,7 +60,7 @@ L6:
4 mark({ field = v }) INIT: in: {a=D, v=ID} out: {a=D, v=ID} USE: in: {field=ONLY_WRITTEN_NEVER_READ, v=READ} out: {field=ONLY_WRITTEN_NEVER_READ, v=READ}
r(v) -> <v1> USE: in: {field=ONLY_WRITTEN_NEVER_READ} out: {field=ONLY_WRITTEN_NEVER_READ, v=READ}
w(field|<v1>) INIT: in: {a=D, v=ID} out: {a=D, field=I, v=ID} USE: in: {} out: {field=ONLY_WRITTEN_NEVER_READ}
L7:
L8:
3 <END> INIT: in: {a=D, field=I, v=ID} out: {a=D, field=I, v=ID}
error:
<ERROR> INIT: in: {} out: {}
@@ -14,11 +14,13 @@ fun f() {
L0:
1 <START>
2 mark({ class LocalClass() { fun f() { val x = "" fun loc() { val x3 = "" } } } })
jmp?(L2) NEXT:[<END>, d(fun f() { val x = "" fun loc() { val x3 = "" } })]
jmp?(L2) NEXT:[<END>, jmp?(L3)]
jmp?(L3) NEXT:[<END>, d(fun f() { val x = "" fun loc() { val x3 = "" } })]
d(fun f() { val x = "" fun loc() { val x3 = "" } }) NEXT:[<SINK>]
L1:
L2 [after local declaration]:
1 <END> NEXT:[<SINK>] PREV:[jmp?(L2)]
L2 [after local class]:
L3 [after local declaration]:
1 <END> NEXT:[<SINK>] PREV:[jmp?(L2), jmp?(L3)]
error:
<ERROR> PREV:[]
sink:
@@ -33,18 +35,18 @@ fun f() {
}
}
---------------------
L3:
L4:
3 <START>
4 mark({ val x = "" fun loc() { val x3 = "" } })
v(val x = "")
mark("")
r("") -> <v0>
w(x|<v0>)
jmp?(L5) NEXT:[<END>, d(fun loc() { val x3 = "" })]
jmp?(L6) NEXT:[<END>, d(fun loc() { val x3 = "" })]
d(fun loc() { val x3 = "" }) NEXT:[<SINK>]
L4:
L5 [after local declaration]:
3 <END> NEXT:[<SINK>] PREV:[jmp?(L5)]
L5:
L6 [after local declaration]:
3 <END> NEXT:[<SINK>] PREV:[jmp?(L6)]
error:
<ERROR> PREV:[]
sink:
@@ -55,17 +57,17 @@ fun loc() {
val x3 = ""
}
---------------------
L6:
L7:
5 <START>
6 mark({ val x3 = "" })
v(val x3 = "")
mark("")
r("") -> <v0>
w(x3|<v0>)
L7:
L8:
5 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
=====================
@@ -12,12 +12,14 @@ fun foo() {
L0:
1 <START>
2 mark({ class B { val a: Int get() { val b: Int return b } } })
jmp?(L2) NEXT:[<END>, v(val a: Int get() { val b: Int return b })]
v(val a: Int get() { val b: Int return b })
jmp?(L2) NEXT:[<END>, d(get() { val b: Int return b })]
jmp?(L3) NEXT:[<END>, d(get() { val b: Int return b })]
d(get() { val b: Int return b }) NEXT:[<SINK>]
L1:
L2 [after local declaration]:
1 <END> NEXT:[<SINK>] PREV:[jmp?(L2)]
L2 [after local class]:
L3 [after local declaration]:
1 <END> NEXT:[<SINK>] PREV:[jmp?(L2), jmp?(L3)]
error:
<ERROR> PREV:[]
sink:
@@ -29,16 +31,16 @@ get() {
return b
}
---------------------
L3:
L4:
3 <START>
4 mark({ val b: Int return b })
v(val b: Int)
r(b) -> <v0>
ret(*|<v0>) L4
L4:
ret(*|<v0>) L5
L5:
3 <END> NEXT:[<SINK>]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
=====================
@@ -23,7 +23,7 @@ fun testObjectExpression1() {
fun testClassDeclaration() {
class C : <!UNREACHABLE_CODE!>Foo(<!>todo()<!UNREACHABLE_CODE!>)<!> {}
<!UNREACHABLE_CODE!>bar()<!>
bar()
}
fun testFunctionDefaultArgument() {
@@ -0,0 +1,21 @@
fun foo() {
var x: String
class A {
init {
x = ""
}
}
// Error! See KT-10042
<!UNINITIALIZED_VARIABLE!>x<!>.length
}
fun bar() {
var x: String
object: Any() {
init {
x = ""
}
}
// Ok
x.length
}
@@ -0,0 +1,4 @@
package
public fun bar(): kotlin.Unit
public fun foo(): kotlin.Unit
@@ -94,6 +94,12 @@ public class DataFlowTestGenerated extends AbstractDataFlowTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg-variables/bugs"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("initializationInLocalClass.kt")
public void testInitializationInLocalClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/initializationInLocalClass.kt");
doTest(fileName);
}
@TestMetadata("kt10243.kt")
public void testKt10243() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/kt10243.kt");
@@ -840,6 +840,12 @@ public class PseudoValueTestGenerated extends AbstractPseudoValueTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/cfg-variables/bugs"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("initializationInLocalClass.kt")
public void testInitializationInLocalClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/initializationInLocalClass.kt");
doTest(fileName);
}
@TestMetadata("kt10243.kt")
public void testKt10243() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cfg-variables/bugs/kt10243.kt");
@@ -2895,6 +2895,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("initializationInLocalClass.kt")
public void testInitializationInLocalClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/initializationInLocalClass.kt");
doTest(fileName);
}
@TestMetadata("initializationInLocalFun.kt")
public void testInitializationInLocalFun() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlFlowAnalysis/initializationInLocalFun.kt");