KT-12928: decompose invocation like "a.foo(b)" to "$t = a; $s = $t.foo.bind($t); $s(b)" instead of "$t = a.foo; $t(b)", since in the latter case foo won't receive proper this. Add optimization that replaces "foo.bar.bind(baz)(args)" with "baz.bar(args)"

This commit is contained in:
Alexey Andreev
2016-07-01 12:14:05 +03:00
parent 07cb3e09b9
commit 88eef7337e
11 changed files with 157 additions and 3 deletions
@@ -0,0 +1,8 @@
package foo
fun box(): String {
val v = mapOf(1 to "1", 2 to "2").mapValues { it.value.map { it.toString() } }
assertEquals(2, v.size)
return "OK"
}
@@ -0,0 +1,9 @@
package lib
var global = ""
inline fun baz(x: () -> Int) = A(1).bar(x())
class A(val y: Int) {
fun bar(x: Int) = x + y
}
@@ -0,0 +1,20 @@
package foo
import lib.*
fun qqq(): Int {
global += "qqq;"
return 23
}
fun box(): String {
assertEquals(24, baz {
global += "before;"
val result = qqq()
global += "after;"
result
})
assertEquals("before;qqq;after;", global)
return "OK"
}
@@ -0,0 +1,11 @@
package lib
var global = ""
inline fun baz(x: () -> Int) = ((A(1).B(x()) as Any) as A.B).bar()
class A(val y: Int) {
inner class B(val x: Int) {
fun bar() = x + y
}
}
@@ -0,0 +1,20 @@
package foo
import lib.*
fun qqq(): Int {
global += "qqq;"
return 23
}
fun box(): String {
assertEquals(24, baz {
global += "before;"
val result = qqq()
global += "after;"
result
})
assertEquals("before;qqq;after;", global)
return "OK"
}