Lookup for local variables taking into account uninitialized this

Consider a context with uninitialized this, e.g.:

  fun foo() {
    val x = "..."
    class Local(y: String) : Base(L@{ x + y })
  }

Lambda 'L' is an argument of a super class constructor call.
Here 'this@Local' is not initialized yet. Thus local variables captured
in 'Local' can't be used. Instead, they should be captured by lambda 'L'
itself.

Note that lambda 'L' sees both 'x' and 'y' as local variables that
should be captured.

When in context with uninitialized this (generating arguments for super
type constructor or delegating constructor call), and a variable in
question is not found in the current context, use enclosing local lookup
to determine whether a local variable should be captured by a closure.
This commit is contained in:
Dmitry Petrov
2017-11-02 10:25:46 +03:00
parent ff0a2562e0
commit 4365084645
36 changed files with 973 additions and 442 deletions
@@ -0,0 +1,20 @@
interface Callback {
fun invoke(): String
}
open class Base(val callback: Callback)
class Outer {
val ok = "OK"
inner class Inner1 {
inner class Inner2 : Base(
object : Callback {
override fun invoke() = ok
}
)
}
}
fun box(): String =
Outer().Inner1().Inner2().callback.invoke()