KT-23397 Optimize out field for property delegate when it's safe (JVM)

This commit is contained in:
Pavel Mikhailovskii
2022-06-03 16:54:12 +02:00
committed by teamcity
parent 9ee0268197
commit 65b2cee913
83 changed files with 3015 additions and 19 deletions
@@ -0,0 +1,17 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.test.assertEquals
import kotlin.reflect.jvm.isAccessible
operator fun Any?.getValue(x: Any?, y: Any?): String {
return "OK"
}
val s: String by 1
fun box(): String {
assertEquals(1, ::s.apply { isAccessible = true }.getDelegate())
return "OK"
}
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
operator fun Any?.getValue(x: Any?, y: Any?): String {
return "OK"
}
const val a = "TEXT"
val s: String by a
fun box(): String {
assertEquals("TEXT", ::s.apply { isAccessible = true }.getDelegate())
return "OK"
}
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.test.assertEquals
import kotlin.reflect.jvm.isAccessible
enum class E {
OK, NOT_OK
}
operator fun E.getValue(x: Any?, y: Any?): String = name
val s: String by E.OK
fun box(): String {
assertEquals(E.OK, ::s.apply { isAccessible = true }.getDelegate())
return "OK"
}
@@ -0,0 +1,21 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
object O {
val impl = 123
}
operator fun Any?.getValue(thisRef: Any?, property: KProperty<*>) = "OK"
val s: String by O.impl
fun box(): String {
assertEquals(123, ::s.apply { isAccessible = true }.getDelegate())
return "OK"
}
@@ -0,0 +1,18 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
val impl = 123
operator fun Any?.getValue(thisRef: Any?, property: KProperty<*>) = "OK"
val s: String by impl
fun box(): String {
assertEquals(123, ::s.apply { isAccessible = true }.getDelegate())
return "OK"
}
@@ -0,0 +1,27 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
object Store {
private val map = mutableMapOf<Pair<Any?, KProperty<*>>, String?>()
operator fun getValue(thisRef: Any?, property: KProperty<*>): String? = map[thisRef to property]
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
map[thisRef to property] = value
}
}
object O {
var s: String? by Store
}
fun box(): String {
assertEquals(Store, O::s.apply { isAccessible = true }.getDelegate())
return "OK"
}