K2: Avoid false-positive overload resolution ambiguity with smart casts

The idea is that when we have successful candidates both from smart cast
and original type, we should discriminate in the favor of former ones.

While this problem (see kt55722.kt) existed before this branch is merged,
initially it was recognized on FP Ultimate when we stopped assuming
captured types from the same projections as equal (see kt55722Initial.kt).

^KT-55722 Fixed
^KT-55024 Fixed
^KT-56283 Related
^KT-56310 Related
This commit is contained in:
Denis.Zharkov
2023-01-02 12:00:14 +01:00
committed by Space Team
parent 7b6c6fceb6
commit b6b132a9a3
27 changed files with 1034 additions and 35 deletions
@@ -0,0 +1,18 @@
FILE: kt55722.kt
public abstract interface A<T> : R|kotlin/Any| {
public open fun foo(x: R|T?|): R|kotlin/Unit| {
}
}
public abstract interface B<F> : R|A<F>| {
public abstract override fun foo(x: R|F?|): R|kotlin/Unit|
}
public final fun <T> bar(x: R|A<in T>|): R|kotlin/Unit| {
when () {
(R|<local>/x| is R|B<in T>|) -> {
R|<local>/x|.R|SubstitutionOverride</B.foo: R|kotlin/Unit|>|(Null(null))
}
}
}
@@ -0,0 +1,17 @@
// FIR_IDENTICAL
// FIR_DUMP
// SKIP_TXT
interface A<T> {
fun foo(x: T?) {}
}
interface B<F> : A<F> {
override fun foo(x: F?)
}
fun <T> bar(x: A<in T>) {
if (x is B) {
x.foo(null) // Shouldn't be OVERLOAD_RESOLUTION_AMBIGUITY
}
}
@@ -0,0 +1,14 @@
FILE: kt55722Initial.kt
public abstract interface MyConsumer<T> : R|kotlin/Any| {
public open fun consume(x: R|T|): R|kotlin/Unit| {
}
}
public final fun foo(x: R|MyConsumer<in kotlin/CharSequence>?|, v: R|kotlin/CharSequence|): R|kotlin/Unit| {
when () {
!=(R|<local>/x|, Null(null)) -> {
R|<local>/x|.R|SubstitutionOverride</MyConsumer.consume: R|kotlin/Unit|>|(R|<local>/v|)
}
}
}
@@ -0,0 +1,13 @@
// FIR_IDENTICAL
// FIR_DUMP
// SKIP_TXT
interface MyConsumer<T> {
fun consume(x: T) {}
}
fun foo(x: MyConsumer<in CharSequence>?, v: CharSequence) {
if (x != null) {
x.consume(v)
}
}