FIR: Report CAPTURED_VAL_INITIALIZATION if initialization is done

outside of constructor.

In other words, when EXACTLY_ONCE lambda initializes non-static final
field of a class, the lambda has to be inlined, otherwise, backend
generated separate function of separate class for the lambda,
which lead to exception at runtime.

 #KT-64854 Fixed
 #KT-59906 Fixed
This commit is contained in:
Ilmir Usmanov
2024-03-05 22:00:19 +01:00
committed by Space Team
parent 5dec87eba8
commit 1c4023fda5
7 changed files with 149 additions and 10 deletions
@@ -42,6 +42,11 @@ class Test {
val b: String
val c: String
val d: String
val e: String
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>val f: String<!>
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>val g: String<!>
val h: String
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>val i: String<!>
init {
inlineMe {
@@ -56,6 +61,35 @@ class Test {
notinline {
<!CAPTURED_VAL_INITIALIZATION!>d<!> = "not allowed"
}
crossinlineMe {
inlineMe {
e = "not allowed"
}
}
fun localFun() {
<!CAPTURED_MEMBER_VAL_INITIALIZATION!>f<!> = "not allowed"
}
val <!UNUSED_VARIABLE!>localLambda<!> = {
<!CAPTURED_MEMBER_VAL_INITIALIZATION!>g<!> = "not allowed"
}
object {
val o: String
init {
h = "not allowed"
o = "allowed"
}
}
class Local {
init {
<!CAPTURED_MEMBER_VAL_INITIALIZATION!>i<!> = "not allowed"
}
}
}
}
@@ -141,12 +175,12 @@ class Test5 {
a = "OK"
}
val bInit = crossinlineMe {
<!CAPTURED_VAL_INITIALIZATION!>b<!> = "OK"
<!CAPTURED_VAL_INITIALIZATION!>b<!> = "not allowed"
}
val cInit = noinlineMe {
<!CAPTURED_VAL_INITIALIZATION!>c<!> = "OK"
<!CAPTURED_VAL_INITIALIZATION!>c<!> = "not allowed"
}
val dInit = notinline {
<!CAPTURED_VAL_INITIALIZATION!>d<!> = "OK"
<!CAPTURED_VAL_INITIALIZATION!>d<!> = "not allowed"
}
}