Improve inference on generics for callable references

#KT-10711 Fixed
 #KT-12802 Fixed
 #KT-12964 Fixed
 #KT-15439 Fixed

Analyze callable references in `dependent` mode, then complete them with
respect to expected types
This commit is contained in:
Mikhail Zarechenskiy
2017-01-17 02:30:16 +03:00
parent 07bb7ef4d3
commit 2cac6a9e7d
50 changed files with 938 additions and 22 deletions
@@ -0,0 +1,29 @@
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.test.assertEquals
fun <T, R> foo(x: T): R = TODO()
fun <T> fooReturnInt(x: T): Int = 1
inline fun <reified T, reified R> check(x: T, y: R, f: (T) -> R, tType: String, rType: String) {
assertEquals(tType, T::class.simpleName)
assertEquals(rType, R::class.simpleName)
}
inline fun <reified T, reified R> check(f: (T) -> R, g: (T) -> R, tType: String, rType: String) {
assertEquals(tType, T::class.simpleName)
assertEquals(rType, R::class.simpleName)
}
fun box(): String {
check("", 1, ::foo, "String", "Int")
check("", 1, ::fooReturnInt, "String", "Int")
check("", "", ::fooReturnInt, "String", "Any")
check(Int::toString, ::foo, "Int", "String")
return "OK"
}
@@ -0,0 +1,30 @@
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.test.assertEquals
fun <T, R> foo(x: T): R = TODO()
inline fun <reified T, reified R> bar(x: T, y: R, f: (T) -> R, tType: String, rType: String): Pair<T, R?> {
assertEquals(tType, T::class.simpleName)
assertEquals(rType, R::class.simpleName)
return Pair(x, y)
}
data class Pair<A, B>(val a: A, val b: B)
fun box(): String {
bar(1, "", ::foo, "Int", "String")
val s1: Pair<Int, String?> = bar(1, "", ::foo, "Int", "String")
val (a: Int, b: String?) = bar(1, "", ::foo, "Int", "String")
val ns: String? = null
bar(ns, ns, ::foo, "String", "String")
val s2: Pair<Int?, String?> = bar(null, null, ::foo, "Int", "String")
return "OK"
}
@@ -0,0 +1,23 @@
// IGNORE_BACKEND: JS
// WITH_RUNTIME
// WITH_REFLECT
import kotlin.test.assertEquals
fun foo(x: Int?) {}
fun foo(y: String?) {}
fun foo(z: Boolean) {}
inline fun <reified T> bar(f: (T) -> Unit, tType: String): T? {
assertEquals(tType, T::class.simpleName)
return null
}
fun box(): String {
val a1: Int? = bar(::foo, "Int")
val a2: String? = bar(::foo, "String")
val a3: Boolean? = bar<Boolean>(::foo, "Boolean")
return "OK"
}
@@ -0,0 +1,8 @@
// WITH_RUNTIME
class Wrapper<T>(val value: T)
fun box(): String {
val ls = listOf("OK").map(::Wrapper)
return ls[0].value
}
@@ -0,0 +1,29 @@
fun baz(i: Int) = i
fun <T> bar(x: T): T = x
fun nullableFun(): ((Int) -> Int)? = null
fun box(): String {
val x1: (Int) -> Int = bar(if (true) ::baz else ::baz)
val x2: (Int) -> Int = bar(nullableFun() ?: ::baz)
val x3: (Int) -> Int = bar(::baz ?: ::baz)
val i = 0
val x4: (Int) -> Int = bar(when (i) {
10 -> ::baz
20 -> ::baz
else -> ::baz
})
val x5: (Int) -> Int = bar(::baz!!)
if (x1(1) != 1) return "fail 1"
if (x2(1) != 1) return "fail 2"
if (x3(1) != 1) return "fail 3"
if (x4(1) != 1) return "fail 4"
if (x5(1) != 1) return "fail 5"
if ((if (true) ::baz else ::baz)(1) != 1) return "fail 6"
return "OK"
}