[FIR] Fix capturing of flexible types during resolution

Previously, because we didn't handle flexible types properly in
prepareCapturedType, projections inside flexible types would only be
captured during subtyping with captureStatus=FOR_SUBTYPING
which would lead to the constraint type being wrongly approximated
(see ConstraintInjector.TypeCheckerStateForConstraintInjector
.addNewIncorporatedConstraint).

Fixing the capturing produced two kinds of false positive diagnostics:

1. In ConstraintInjector.TypeCheckerStateForConstraintInjector
.addNewIncorporatedConstraint we would get two instances of cone types
that are structurally equal and containing the same captured type.
However, because we only skipped subtyping if the types were
referentially equal, we would get a contradiction here.
The fix was to use structural equality instead, which should be okay
as the captured type instances are the same.

2. Reified type variables were inferred to captured types because
flexible arrays with captured upper bounds
(Array<Foo>..Array<Captured(out Foo)>?) were not properly approximated.

#KT-62609 Fixed
This commit is contained in:
Kirill Rakhman
2023-11-21 16:51:50 +01:00
committed by Space Team
parent ed4941d9f9
commit 560c1cacf3
16 changed files with 148 additions and 46 deletions
@@ -12,8 +12,16 @@ fun <K> select(x: K, y: K): K = x
fun <R> foo(f: () -> R): R = f()
interface Inv<T> {
fun createArray(): Array<T>
}
fun test(n: Number) {
val a = select(foo { JavaTest.createNumberArray() }, emptyArray())
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Array<kotlin.Number..kotlin.Number?!>..kotlin.Array<out kotlin.Number..kotlin.Number?!>?!")!>a<!>
}
a
}
fun test2(inv: Inv<out CharSequence>) {
val a = select(foo { inv.createArray() }, emptyArray())
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Array<out kotlin.CharSequence>")!>a<!>
}
@@ -12,8 +12,16 @@ fun <K> select(x: K, y: K): K = x
fun <R> foo(f: () -> R): R = f()
interface Inv<T> {
fun createArray(): Array<T>
}
fun test(n: Number) {
val a = select(foo { JavaTest.createNumberArray() }, emptyArray())
<!DEBUG_INFO_EXPRESSION_TYPE("(kotlin.Array<(kotlin.Number..kotlin.Number?)>..kotlin.Array<out (kotlin.Number..kotlin.Number?)>?)")!>a<!>
}
}
fun test2(inv: Inv<out CharSequence>) {
val a = select(foo { inv.createArray() }, emptyArray())
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Array<out kotlin.CharSequence>")!>a<!>
}
@@ -1,15 +0,0 @@
package
public fun </*0*/ R> foo(/*0*/ f: () -> R): R
public fun </*0*/ K> select(/*0*/ x: K, /*1*/ y: K): K
public fun test(/*0*/ n: kotlin.Number): kotlin.Unit
public open class JavaTest {
public constructor JavaTest()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public open fun createNumberArray(): kotlin.Array<(out) kotlin.Number!>!
}
@@ -0,0 +1,52 @@
// FIR_IDENTICAL
// ISSUE: KT-62609
// FILE: I.java
public interface I<T> {}
// FILE: X.java
public abstract class X<P> {}
// FILE: A.java
public final class A extends X<String> implements I<String> {
public static final A INSTANCE = new A();
}
// FILE: B.java
public final class B extends X<Integer> implements I<Integer> {
public static final B INSTANCE = new B();
}
// FILE: test.kt
fun test1() {
controlFun(A.INSTANCE)
controlFun(B.INSTANCE)
controlFun2(A.INSTANCE)
controlFun2(B.INSTANCE)
val a = when {
true -> A.INSTANCE
else -> B.INSTANCE
}
controlFun(a)
controlFun2(a)
}
fun test2() {
controlFun(A())
controlFun(B())
controlFun2(A())
controlFun2(B())
val a = when {
true -> A()
else -> B()
}
controlFun(a)
controlFun2(a)
}
fun <T> controlFun(c: I<T>) {}
fun <T> controlFun2(c: X<T>) {}