[FIR] Make sure the primary constructor is first in class CFG

The primary constructor of a class needs to be the first subgraph of the
class control-flow graph. Based on the Kotlin specification, class
initialization order goes first primary constructor, in-place
declarations (properties and init blocks), and then secondary
constructors. If the class doesn't have a primary constructor, then it
is just skipped in the order.

Unfortunately, the class control-flow graph had in-place declarations
first and then all constructors. Instead, we should treat the primary
constructor as the first in-place declaration, and then continue with
the existing processing as secondary constructors. This will guarantee
that super constructor calls have the correct property initialization
information.

^KT-65093 Fixed
This commit is contained in:
Brian Norman
2024-01-18 20:17:17 -06:00
committed by Space Team
parent c628172235
commit 17a1871b83
28 changed files with 732 additions and 900 deletions
@@ -3,6 +3,6 @@ open class A(val a: Any) {
override fun toString() = a.toString()
}
object B : A(B.foo) { // call B.foo should be not-allowed
object B : A(<!UNINITIALIZED_VARIABLE!>B.foo<!>) { // call B.foo should be not-allowed
val foo = 4
}
@@ -2,8 +2,8 @@
fun error(): Nothing = throw Exception()
class Some<!UNREACHABLE_CODE!>()<!> {
var x: Int
class Some() {
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>var x: Int<!>
val y: Int = error()
init {
@@ -1,222 +0,0 @@
fun foo(): Int = 42
object ThrowInTryWithCatch {
private val p: String
init {
try {
throw Exception()
} catch (e: Exception) {
}
p = "OK"
}
}
object ThrowInTryWithCatchAndFinally {
private val p: String
init {
try {
throw Exception()
} catch (e: Exception) {
} finally {
}
p = "OK"
}
}
object ThrowInFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
} finally {
throw Exception()
}
p = "OK"
}
}
object RethrowInCatch {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
throw e
}
p = "OK"
}
}
object RethrowInCatchWithFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
throw e
} finally {
}
p = "OK"
}
}
object InnerTryWithCatch {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
throw e
} catch (ee: Exception) {
}
}
p = "OK"
}
}
object InnerTryWithFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
throw e
} finally {
}
}
p = "OK"
}
}
object InnerTryWithCatchAndFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
throw e
} catch (ee: Exception) {
} finally {
}
}
p = "OK"
}
}
object InnerCatch {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} catch (ee: Exception) {
throw ee
}
}
p = "OK"
}
}
object InnerCatchWithFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} catch (ee: Exception) {
throw ee
} finally {
}
}
p = "OK"
}
}
object InnerCatchOuterRethrow {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} catch (ee: Exception) {
throw e
}
}
p = "OK"
}
}
object InnerCatchOuterRethrowWithFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} catch (ee: Exception) {
throw e
} finally {
}
}
p = "OK"
}
}
object InnerFinally {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} finally {
throw e
}
}
p = "OK"
}
}
object InnerFinallyWithCatch {
private val p: String
init {
try {
foo()
} catch (e: Exception) {
try {
foo()
} catch (ee: Exception) {
} finally {
throw e
}
}
p = "OK"
}
}
@@ -1,3 +1,5 @@
// FIR_IDENTICAL
fun foo(): Int = 42
object ThrowInTryWithCatch {
@@ -121,35 +121,35 @@ digraph NestedInnerClass_fir_kts {
35 [label="Enter class Inner [3]" style="filled" fillcolor=red];
subgraph cluster_13 {
color=blue
36 [label="Enter property [4]" style="filled" fillcolor=red];
37 [label="Access variable R|/property| [4]"];
38 [label="Exit property [4]" style="filled" fillcolor=red];
36 [label="Enter function <init> [4]" style="filled" fillcolor=red];
37 [label="Delegated constructor call: super<R|kotlin/Any|>() [4]" style="filled" fillcolor=yellow];
38 [label="Exit function <init> [4]" style="filled" fillcolor=red];
}
subgraph cluster_14 {
color=blue
39 [label="Enter property [4]" style="filled" fillcolor=red];
40 [label="Access variable this@NestedInnerClass# [4]"];
41 [label="Access variable <Unresolved name: property># [4]"];
42 [label="Exit property [4]" style="filled" fillcolor=red];
40 [label="Access variable R|/property| [4]"];
41 [label="Exit property [4]" style="filled" fillcolor=red];
}
subgraph cluster_15 {
color=blue
43 [label="Enter function <init> [4]" style="filled" fillcolor=red];
44 [label="Delegated constructor call: super<R|kotlin/Any|>() [4]" style="filled" fillcolor=yellow];
45 [label="Exit function <init> [4]" style="filled" fillcolor=red];
42 [label="Enter property [4]" style="filled" fillcolor=red];
43 [label="Access variable this@NestedInnerClass# [4]"];
44 [label="Access variable <Unresolved name: property># [4]"];
45 [label="Exit property [4]" style="filled" fillcolor=red];
}
46 [label="Exit class Inner [3]" style="filled" fillcolor=red];
}
35 -> {36} [color=green];
35 -> {46} [style=dotted];
35 -> {36 39 43} [style=dashed];
35 -> {36 39 42} [style=dashed];
36 -> {37};
37 -> {38};
38 -> {39} [color=green];
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43} [color=green];
41 -> {42} [color=green];
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46} [color=green];
@@ -121,35 +121,35 @@ digraph NestedInnerClass_ll_kts {
35 [label="Enter class Inner [3]" style="filled" fillcolor=red];
subgraph cluster_13 {
color=blue
36 [label="Enter property [4]" style="filled" fillcolor=red];
37 [label="Access variable R|/property| [4]"];
38 [label="Exit property [4]" style="filled" fillcolor=red];
36 [label="Enter function <init> [4]" style="filled" fillcolor=red];
37 [label="Delegated constructor call: super<R|kotlin/Any|>() [4]" style="filled" fillcolor=yellow];
38 [label="Exit function <init> [4]" style="filled" fillcolor=red];
}
subgraph cluster_14 {
color=blue
39 [label="Enter property [4]" style="filled" fillcolor=red];
40 [label="Access variable this@NestedInnerClass# [4]"];
41 [label="Access variable <Unresolved name: property># [4]"];
42 [label="Exit property [4]" style="filled" fillcolor=red];
40 [label="Access variable R|/property| [4]"];
41 [label="Exit property [4]" style="filled" fillcolor=red];
}
subgraph cluster_15 {
color=blue
43 [label="Enter function <init> [4]" style="filled" fillcolor=red];
44 [label="Delegated constructor call: super<R|kotlin/Any|>() [4]" style="filled" fillcolor=yellow];
45 [label="Exit function <init> [4]" style="filled" fillcolor=red];
42 [label="Enter property [4]" style="filled" fillcolor=red];
43 [label="Access variable this@NestedInnerClass# [4]"];
44 [label="Access variable <Unresolved name: property># [4]"];
45 [label="Exit property [4]" style="filled" fillcolor=red];
}
46 [label="Exit class Inner [3]" style="filled" fillcolor=red];
}
35 -> {36} [color=green];
35 -> {46} [style=dotted];
35 -> {36 39 43} [style=dashed];
35 -> {36 39 42} [style=dashed];
36 -> {37};
37 -> {38};
38 -> {39} [color=green];
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43} [color=green];
41 -> {42} [color=green];
42 -> {43};
43 -> {44};
44 -> {45};
45 -> {46} [color=green];