Files
kotlin-fork/compiler/testData/diagnostics/tests/controlFlowAnalysis/initializationInLocalClass.fir.kt
T
pyos 99e51f6940 FIR: check assignments and references to members in constructors
I.e. emit VAL_REASSIGNMENT on repeated assignments to `this.something`,
UNINITIALIZED_VARIABLE on reads of it before any assignment if there is
no initializer, and CAPTURED_MEMBER_VAL_INITIALIZATION on assignments
inside non-called-in-place functions and named classes.

^KT-55528 Fixed
2023-01-26 13:12:13 +00:00

91 lines
1.5 KiB
Kotlin
Vendored

// !DIAGNOSTICS: -ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE
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
}
fun gav() {
val x: String
class B {
init {
// Error! See KT-10445
<!CAPTURED_VAL_INITIALIZATION!>x<!> = ""
}
}
// Error! See KT-10042
<!UNINITIALIZED_VARIABLE!>x<!>.length
val y: String
class C(val s: String) {
constructor(): this("") {
// Error!
<!CAPTURED_VAL_INITIALIZATION!>y<!> = s
}
}
<!UNINITIALIZED_VARIABLE!>y<!>.length
}
open class Gau(val s: String)
fun gau() {
val x: String
object: Any() {
init {
// Ok
x = ""
}
}
// Ok
x.length
val y: String
fun local() {
object: Any() {
init {
// Error!
<!CAPTURED_VAL_INITIALIZATION!>y<!> = ""
}
}
}
val z: String
object: Gau(if (true) {
z = ""
z
}
else "") {}
}
class My {
init {
val x: String
class Your {
init {
// Error! See KT-10445
<!CAPTURED_VAL_INITIALIZATION!>x<!> = ""
}
}
}
}
<!MUST_BE_INITIALIZED!>val top: Int<!>
fun init() {
<!VAL_REASSIGNMENT!>top<!> = 1
}