[FIR] Support completion of lambdas with type variable as expected type

#KT-37310 Fixed
#KT-37304 Fixed
This commit is contained in:
Dmitriy Novozhilov
2020-03-06 14:11:31 +03:00
parent 643d7be12d
commit 1c0fd7342f
28 changed files with 281 additions and 186 deletions
@@ -6,7 +6,7 @@ fun testLambda() {
if (x is String) return@myRun { it -> x.length <!AMBIGUITY!>+<!> it }
if (x !is Int) return@myRun { it -> it }
{ it -> x <!AMBIGUITY!>+<!> it }
{ it -> x + it }
}
val twoLambda: (Int) -> Int = myRun {
@@ -1,26 +0,0 @@
//KT-3184 Type inference seems partially broken
package a
import java.util.HashMap
private fun <T> test(value: T, extf: String.(value: T)->Unit) {
"".extf(value)
}
fun main() {
test(1, {value -> println(value)})
}
fun tests() {
val dict = HashMap<String, (String) -> Unit>()
<!INAPPLICABLE_CANDIDATE!>dict["0"] = { str -> println(str) }<!>
<!INAPPLICABLE_CANDIDATE!>dict["1"] = { println(<!UNRESOLVED_REFERENCE!>it<!>) }<!>
dict.<!INAPPLICABLE_CANDIDATE!>set<!>("1", { println(<!UNRESOLVED_REFERENCE!>it<!>) })
<!INAPPLICABLE_CANDIDATE!>dict["1"] = { r -> println(r) }<!>
}
// from standard library
operator fun <K, V> MutableMap<K, V>.set(key : K, value : V) : V? = this.put(key, value)
fun println(message : Any?) = System.out.println(message)
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-3184 Type inference seems partially broken
package a
@@ -1,37 +0,0 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
class B {
class Builder
}
typealias ApplyRestrictions = B.Builder.() -> B.Builder
fun applyRestrictions1(): ApplyRestrictions = TODO()
fun applyRestrictions2() = applyRestrictions1()
fun <K> applyRestrictions3(e: K) = applyRestrictions1()
fun buildB() {
val a1 = applyRestrictions1()
val a2 = applyRestrictions2()
val a3 = applyRestrictions3("foo")
B.Builder().a1()
B.Builder().a2()
B.Builder().a3()
}
// additional example from #KT-34820
class R
class P
typealias F = R.(P) -> Unit
fun guess(): F? = TODO()
fun consume(f: F) {}
fun problem() {
val p = guess()
<!INAPPLICABLE_CANDIDATE!>consume<!>(p ?: {})
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER
@@ -3,6 +3,6 @@
// See EA-76890 / KT-10843: NPE during analysis
fun lambda(x : Int?) = x?.<!UNRESOLVED_REFERENCE!>let<!> <!UNRESOLVED_REFERENCE!>l<!> {
y ->
if (y <!UNRESOLVED_REFERENCE!>><!> 0) return@l x
if (y > 0) return@l x
y
}!!
@@ -38,7 +38,7 @@ class W4(val f: L2) {
fun test1() { // to extension lambda 0
val w10 = W1 { this } // oi+ ni+
val i10: E0 = id { this } // o1- ni+
val j10 = <!INAPPLICABLE_CANDIDATE!>id<!><E0> { this } // oi+ ni+
val j10 = id<E0> { this } // oi+ ni+
val f10 = W1(fun Int.(): Int = this) // oi+ ni+
val g10: E0 = id(fun Int.(): Int = this) // oi+ ni+
@@ -46,7 +46,7 @@ fun test1() { // to extension lambda 0
val i11: E0 = id { i: Int -> i } // o1+ ni+
val w12 = <!INAPPLICABLE_CANDIDATE!>W1<!> { i -> i } // oi- ni-
val i12: E0 = id { i -> i } // oi- ni-
val j12 = <!INAPPLICABLE_CANDIDATE!>id<!><E0> { i -> i } // oi- ni-
val j12 = id<E0> { i -> i } // oi- ni-
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w13 = W1 { it } // this or it: oi- ni-
@@ -79,7 +79,7 @@ fun test2() { // to extension lambda 1
// val i27: E1 = id { i, s: String -> i + s.length } // overload oi- ni-
val w28 = <!INAPPLICABLE_CANDIDATE!>W2<!> { i: Int, s -> i <!AMBIGUITY!>+<!> s.<!UNRESOLVED_REFERENCE!>length<!> } // oi- ni-
val i28: E1 = id { i: Int, s -> i <!AMBIGUITY!>+<!> s.<!UNRESOLVED_REFERENCE!>length<!> } // oi- ni-
val i28: E1 = id { i: Int, s -> i + s.length } // oi- ni-
val w29 = <!INAPPLICABLE_CANDIDATE!>W2<!> { i: Int, s: String -> i + s.length } // oi- ni-
val i29: E1 = id { i: Int, s: String -> i + s.length } // oi+ ni+
@@ -92,8 +92,8 @@ fun test3() { // to non-extension lambda 1
val w30 = W3 { i -> i } // oi+ ni+
val i30: L1 = id { i -> i } // oi+ ni+
val w31 = W3 { it } // oi+ ni+
val i31: L1 = id { <!UNRESOLVED_REFERENCE!>it<!> } // oi- ni+
val j31 = <!INAPPLICABLE_CANDIDATE!>id<!><L1> { <!UNRESOLVED_REFERENCE!>it<!> } // oi+ ni+
val i31: L1 = id { it } // oi- ni+
val j31 = id<L1> { it } // oi+ ni+
// yet unsupported cases - considering lambdas as extension ones unconditionally
// val w32 = W3 { this } // this or it: oi- ni-
@@ -1,32 +0,0 @@
//If this test hangs, it means something is broken.
object A {
val iii = 42
}
//inappropriate but participating in resolve functions
fun foo(s: String, a: Any) = s + a
fun foo(a: Any) = a
fun foo(i: Int) = i
fun foo(a: Any, i: Int, f: ()-> Int) = "$a$i${f()}"
fun foo(f: (Int)->Int, i: Int) = f(i)
fun foo(f: (String)->Int, s: String) = f(s)
fun foo(f: (Any)->Int, a: Any) = f(a)
fun foo(s: String, f: (String, String)->Int) = f(s, s)
//appropriate function
fun foo(i: Int, f: (Int)->Int) = f(i)
fun <T> id(t: T) = t
fun test() {
<!INAPPLICABLE_CANDIDATE!>foo<!>(1, id { x1 ->
<!INAPPLICABLE_CANDIDATE!>foo<!>(2, id { x2 ->
<!INAPPLICABLE_CANDIDATE!>foo<!>(3, id { x3 ->
<!INAPPLICABLE_CANDIDATE!>foo<!>(4, id { x4 ->
<!INAPPLICABLE_CANDIDATE!>foo<!>(5, id { x5 ->
x1 + x2 + x3 + x4 + x5 + A.iii
})
})
})
})
})
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//If this test hangs, it means something is broken.
object A {
val iii = 42
@@ -22,11 +22,11 @@ fun foo(i: Int, f: (Int) -> Int) = f(i)
fun <T> id(t: T) = t
fun test() {
foo(1, id(fun(x1: Int) =
foo(2, id(fun(x2: Int) =
foo(3, id(fun(x3: Int) =
foo(4, id(fun(x4: Int) =
foo(5, id(fun(x5: Int) =
<!AMBIGUITY!>foo<!>(1, id(fun(x1: Int) =
<!AMBIGUITY!>foo<!>(2, id(fun(x2: Int) =
<!AMBIGUITY!>foo<!>(3, id(fun(x3: Int) =
<!AMBIGUITY!>foo<!>(4, id(fun(x4: Int) =
<!AMBIGUITY!>foo<!>(5, id(fun(x5: Int) =
x1 + x2 + x3 + x4 + x5 + A.iii
))
))