Resolve invoke on any kind of expressions, not only on simple name expressions

This commit is contained in:
Svetlana Isakova
2014-02-04 19:07:06 +04:00
parent c7087d170e
commit a829da185d
31 changed files with 391 additions and 101 deletions
@@ -0,0 +1,21 @@
class A
class B {
fun A.invoke() = "##"
fun A.invoke(i: Int) = "#${i}"
}
fun foo() = A()
fun B.test(): String {
if (A()() != "##") return "fail1"
if (A()(1) != "#1") return "fail2"
if (foo()() != "##") return "fail3"
if (foo()(42) != "#42") return "fail4"
if ((foo())(42) != "#42") return "fail5"
if ({() -> A()}()() != "##") return "fail6"
if ({() -> A()}()(37) != "#37") return "fail7"
return "OK"
}
fun box(): String = B().test()
@@ -0,0 +1,20 @@
//KT-3217 Invoke convention after function invocation doesn't work
//KT-2728 Can't compile A()()
class A {
fun invoke() = "##"
fun invoke(i: Int) = "#${i}"
}
fun foo() = A()
fun box(): String {
if (A()() != "##") return "fail1"
if (A()(1) != "#1") return "fail2"
if (foo()() != "##") return "fail3"
if (foo()(42) != "#42") return "fail4"
if ((foo())(42) != "#42") return "fail5"
if ({() -> A()}()() != "##") return "fail6"
if ({() -> A()}()(37) != "#37") return "fail7"
return "OK"
}
@@ -0,0 +1,13 @@
//KT-3450 get and invoke are not parsed in one expression
public class A(val s: String) {
public fun get(i: Int) : A = A("$s + $i")
public fun invoke(builder : A.() -> String): String = builder()
}
fun x(y : String) : A = A(y)
fun foo() = x("aaa")[42] { "$s!!" }
fun box() = if (foo() == "aaa + 42!!") "OK" else "fail"
@@ -0,0 +1,5 @@
//KT-3631 String.invoke doesn't work with literals
fun String.invoke(i: Int) = "$this$i"
fun box() = if ("a"(12) == "a12") "OK" else "fail"
@@ -0,0 +1,21 @@
//KT-3772 Invoke and overload resolution ambiguity
open class A {
fun invoke(f: A.() -> Unit) = 1
}
class B {
fun invoke(f: B.() -> Unit) = 2
}
open class C
val C.attr = A()
open class D: C()
val D.attr = B()
fun box(): String {
val d = D()
return if (d.attr {} == 2) "OK" else "fail"
}
@@ -0,0 +1,8 @@
//KT-3821 Invoke convention doesn't work for `this`
class A() {
fun invoke() = 42
fun foo() = this() // Expecting a function type, but found A
}
fun box() = if (A().foo() == 42) "OK" else "fail"
@@ -0,0 +1,9 @@
//KT-3822 Compiler crashes when use invoke convention with `this` in class which extends Function0<T>
class B() : Function0<Boolean> {
override fun invoke() = true
fun foo() = this() // Exception
}
fun box() = if (B().foo()) "OK" else "fail"