Restore anonymous classes for local delegate metadatas in inline functions

The problem is that now that the local delegated property metadata is in
the $$delegatedProperties array of the containing class, the access to
it from code calling an inline function with a local delegated property
is illegal.

Currently it seems to be a lot of work to support this rather rare case
properly (see the comment in ExpressionCodegen.getVariableMetadataValue)
so we postpone it and return the old behavior of using the anonymous
KProperty subclass for metadata
This commit is contained in:
Alexander Udalov
2017-06-14 13:21:09 +03:00
parent 616d575fb6
commit 2611c7de7e
14 changed files with 216 additions and 14 deletions
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM
// WITH_REFLECT
import kotlin.reflect.*
import kotlin.test.assertEquals
object Delegate {
lateinit var property: KProperty<*>
operator fun getValue(instance: Any?, kProperty: KProperty<*>) {
property = kProperty
}
}
class Foo {
inline fun foo() {
val x by Delegate
x
}
}
fun box(): String {
Foo().foo()
assertEquals("val x: kotlin.Unit", Delegate.property.toString())
return "OK"
}
@@ -0,0 +1,26 @@
// FILE: 1.kt
package test
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): String = "O"
}
inline fun test(crossinline s: () -> String): String {
val delegate = Delegate()
val o = object {
fun run(): String {
val prop: String by delegate
return prop + s()
}
}
return o.run()
}
// FILE: 2.kt
import test.*
fun box(): String {
return test { "K" }
}
@@ -0,0 +1,23 @@
// FILE: 1.kt
package test
import kotlin.reflect.KProperty
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): String = "OK"
}
inline fun test(): String {
val b by Delegate()
return run {
b
}
}
// FILE: 2.kt
import test.*
fun box(): String {
return test()
}