K2: Change resolution rules of when/if expressions with expected type

When expected type is known, use it as expected type for branch bodies.
While it indeed becomes different from the usual select call resolution,
where expected type is applied only after completion starts,
it helps to support, e.g. callable references resolution just as powerful
as it was in K1.

Also, in some cases where diagnostics have been changed, they become
a bit more helpful since they are reported closer
to the problematic places

cannotCastToFunction.kt test has been removed because it relied
on the case erroneously supported by the hack removed from
the FirCallResolver in this commit.

^KT-45989 Fixed
^KT-55936 Fixed
^KT-56445 Fixed
^KT-54709 Related
^KT-55931 Related
This commit is contained in:
Denis.Zharkov
2022-11-29 13:54:07 +01:00
committed by Space Team
parent a38040680c
commit 33dcbaac16
26 changed files with 156 additions and 81 deletions
@@ -0,0 +1,30 @@
// SKIP_TXT
fun baz(options: String = ""): String = ""
fun bar(normalizeNames: Boolean = false): String = ""
fun <E> select(x: E, y: E) = x
fun <E> id(e: E): E = e
fun runForString(x: () -> String) {}
val cs: CharSequence = ""
fun foo(dumpStrategy: String) {
val dump0: () -> String = ::baz // TYPE_MISMATCH
val dump1: () -> String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH!>id(::baz)<!> // TYPE_MISMATCH
// OK, TYPE_MISMATCH IN K2
val dump2: () -> String = if (dumpStrategy == "KotlinLike") ::baz else ::bar
val dump3: () -> String = <!INITIALIZER_TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!>select(::baz, ::bar)<!> // TYPE_MISMATCH
var dump4: () -> String = if (dumpStrategy == "KotlinLike") ::baz else ::bar
dump4.invoke()
dump4 = if (dumpStrategy == "KotlinLike") ::baz else ::bar
var dump5: () -> CharSequence = { cs }
expectString(<!ARGUMENT_TYPE_MISMATCH!>dump5.invoke()<!>)
dump5 = if (dumpStrategy == "KotlinLike") ::baz else ::bar
// `dump5` should have smart cast to () -> String
expectString(dump5.invoke())
}
fun expectString(x: String) {}
@@ -0,0 +1,30 @@
// SKIP_TXT
fun baz(options: String = ""): String = ""
fun bar(normalizeNames: Boolean = false): String = ""
fun <E> select(x: E, y: E) = x
fun <E> id(e: E): E = e
fun runForString(x: () -> String) {}
val cs: CharSequence = ""
fun foo(dumpStrategy: String) {
val dump0: () -> String = <!TYPE_MISMATCH!>::<!TYPE_MISMATCH!>baz<!><!> // TYPE_MISMATCH
val dump1: () -> String = <!TYPE_MISMATCH, TYPE_MISMATCH!>id(::baz)<!> // TYPE_MISMATCH
// OK, TYPE_MISMATCH IN K2
val dump2: () -> String = if (dumpStrategy == "KotlinLike") ::baz else ::bar
val dump3: () -> String = <!TYPE_MISMATCH, TYPE_MISMATCH, TYPE_MISMATCH!>select(::baz, ::bar)<!> // TYPE_MISMATCH
var dump4: () -> String = if (dumpStrategy == "KotlinLike") ::baz else ::bar
dump4.invoke()
dump4 = if (dumpStrategy == "KotlinLike") ::baz else ::bar
var dump5: () -> CharSequence = { cs }
expectString(<!TYPE_MISMATCH!>dump5.invoke()<!>)
dump5 = if (dumpStrategy == "KotlinLike") ::baz else ::bar
// `dump5` should have smart cast to () -> String
expectString(<!DEBUG_INFO_SMARTCAST!>dump5<!>.invoke())
}
fun expectString(x: String) {}
@@ -0,0 +1,14 @@
// FIR_IDENTICAL
// SKIP_TXT
// WITH_STDLIB
// ISSUE: KT-56445
fun foo(x: Int?, y: Int = 1) {}
fun bar() {}
fun test() {
mapOf( // String, KFunction<Unit>
"a" to ::foo,
"b" to ::bar,
)
}
@@ -10,7 +10,7 @@ fun dump(dumpStrategy: String) {
val f0: Function0<String> = <!INAPPLICABLE_CANDIDATE!>returnAdapter<!>(::<!UNRESOLVED_REFERENCE!>foo<!>)
val f1: Function0<String> = ::foo
val f2: Function0<String> = if (dumpStrategy == "KotlinLike") ::<!UNRESOLVED_REFERENCE!>foo<!> else ::<!UNRESOLVED_REFERENCE!>bar<!>
val f2: Function0<String> = if (dumpStrategy == "KotlinLike") ::foo else ::bar
}
fun returnAdapter(a: kotlin.reflect.KFunction0<String>) = a