K2: allow/disallow coercion-to-unit for callable references more precisely

Before this commit, K2 always applied coercion-to-unit for
callable references if expected type was Unit, and actual non-Unit.
However, this may not work in case when actual return type is
a type parameter and it must be inferred into Unit.
In this commit we started to disallow coercion-to-unit
for references with synthetic outer call (~ top-level in K1)
AND a type parameter as a return type (both should be true to disallow).
This provides better K1 consistency,
while still keeping some broken K1 cases working in K2.

See also added comment in CallableReferenceResolution.kt.

#KT-62565 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-12-05 15:12:31 +01:00
committed by Space Team
parent 25c0bd278e
commit 2680c8effd
16 changed files with 213 additions and 16 deletions
@@ -0,0 +1,8 @@
fun foo(x: Int, y: Any): Int = x
fun <T> bar(x: T, y: Any): T = x
fun main() {
val fooRef: (Int, Any) -> Unit = ::foo
val barRef: (Int, Any) -> Unit = ::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>bar<!>
}
@@ -0,0 +1,8 @@
fun foo(x: Int, y: Any): Int = x
fun <T> bar(x: T, y: Any): T = x
fun main() {
val fooRef: (Int, Any) -> Unit = <!TYPE_MISMATCH!>::<!TYPE_MISMATCH!>foo<!><!>
val barRef: (Int, Any) -> Unit = <!TYPE_MISMATCH!>::<!TYPE_MISMATCH!>bar<!><!>
}
@@ -11,7 +11,7 @@ class A1 {
class A2 {
fun <K, V> a2(key: K): V = TODO()
fun test1(): (String) -> Unit = A2()::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a2<!>
fun test1(): (String) -> Unit = A2()::a2
fun <T3> test2(): (T3) -> T3 = A2()::a2
}
@@ -19,7 +19,7 @@ class A3<T> {
fun <V> a3(key: T): V = TODO()
fun test1(): (T) -> Int = this::a3
fun test2(): (T) -> Unit = A3<T>()::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a3<!>
fun test2(): (T) -> Unit = A3<T>()::a3
fun test3(): (Int) -> String = A3<Int>()::a3
fun <R> test4(): (R) -> Unit = <!RETURN_TYPE_MISMATCH!>this::<!NEW_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>a3<!><!>