Files
kotlin/compiler/testData/codegen/bytecodeText/properties/lateinit/companionObject.kt
Alexander Udalov 43413fcc44 Move lateinit assertion for companion property to companion object
Previously, for a property named `x` in the companion object of a class
named `Foo`, we generated:
- `Foo.access$getX$cp`, consisting of `GETFIELD Foo.x` and lateinit
  assertion
- `Foo.Companion.getX`, consisting of `INVOKEVIRTUAL Foo.access$getX$cp`

Now, we generate:
- `Foo.access$getX$cp`, consisting of `GETFIELD Foo.x`
- `Foo.Companion.getX`, consisting of `INVOKEVIRTUAL Foo.access$getX$cp`
  and lateinit assertion

The reason is that this way we can avoid generating another accessor and
reuse `Foo.access$getX$cp` in case `isInitialized` is called on a
lateinit property from companion.

For private properties, getX is not generated, but instead the assertion
is generated on each access to the field (which can be improved, see
KT-28331). The same happens for access to non-private properties from
inside the same context where they're declared.

 #KT-21862 In Progress
2018-12-13 14:12:33 +01:00

25 lines
462 B
Kotlin
Vendored

class Foo {
private companion object {
lateinit var x: String
fun test() {
consume(x)
consume(x)
consume(x)
consume(x)
}
}
fun test2() {
consume(x)
consume(x)
consume(x)
consume(x)
}
}
fun consume(s: String) {}
// There's 1 assertion in Foo.Companion.getX, and 4 in Foo.test2 (see KT-28331)
// 5 throwUninitializedPropertyAccessException