KT-3189 Function invoke is called with no reason

prioritize tasks specially for invoke

 #KT-3189 Fixed
 #KT-3190 Fixed
 #KT-3297 Fixed
This commit is contained in:
Svetlana Isakova
2013-07-04 14:14:35 +04:00
parent 9347a48df8
commit cf5c5dba3d
11 changed files with 567 additions and 70 deletions
@@ -0,0 +1,28 @@
package invoke
fun test1(predicate: (Int) -> Int, i: Int) = predicate(i)
fun test2(predicate: (Int) -> Int, i: Int) = predicate.invoke(i)
class Method {
fun invoke(i: Int) = i
}
fun test3(method: Method, i: Int) = method.invoke(i)
fun test4(method: Method, i: Int) = method(i)
class Method2 {}
fun Method2.invoke(s: String) = s
fun test5(method2: Method2, s: String) = method2(s)
fun box() : String {
if (test1({ it }, 1) != 1) return "fail 1"
if (test2({ it }, 2) != 2) return "fail 2"
if (test3(Method(), 3) != 3) return "fail 3"
if (test4(Method(), 4) != 4) return "fail 4"
if (test5(Method2(), "s") != "s") return "fail5"
return "OK"
}
@@ -0,0 +1,15 @@
//KT-3189 Function invoke is called with no reason
fun box(): String {
val bad = Bad({ 1 })
return if (bad.test() == 1) "OK" else "fail"
}
class Bad(val a: () -> Int) {
fun test(): Int = a()
fun invoke(): Int = 2
}
@@ -0,0 +1,24 @@
//KT-3190 Compiler crash if function called 'invoke' calls a closure
fun box(): String {
val test = Cached<Int,Int>({ it + 2 })
return if (test(1) == 3) "OK" else "fail"
}
class Cached<K, V>(private val generate: (K)->V): jet.Function1<K, V> {
val store = java.util.HashMap<K, V>()
// Everything works just fine if 'invoke' method is renamed to, for example, 'get'
override fun invoke(p1: K) = store.getOrPut(p1) { generate(p1) }
}
//from library
fun <K,V> MutableMap<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
if (this.containsKey(key)) {
return this.get(key) as V
} else {
val answer = defaultValue()
this.put(key, answer)
return answer
}
}
@@ -0,0 +1,17 @@
//KT-3297 Calling the wrong function inside an extension method to the Function0 class
fun <R> Function0<R>.or(alt: () -> R): R {
try {
return this()
} catch (e: Exception) {
return alt()
}
}
fun box(): String {
return {
throw RuntimeException("fail")
} or {
"OK"
}
}