K2: handle recursive types properly in approximation

This commit is intended to deal with inconsistency in K1/K2
star projection handling.
K1 star projection includes a 'type' property.
This type from a star projection can be used for relevant
functions / properties return types,
and already includes some approximation for recursive generics.
In contrast, K2 star projection is an object,
and return types of relevant functions / properties are
represented as captured types.
To prevent recursion in them in recursive generic case,
this commit includes additional replacement of their type arguments.
See more details in added comments.

#KT-65057 Fixed
This commit is contained in:
Mikhail Glukhikh
2024-02-01 15:20:57 +01:00
committed by Space Team
parent c4d6554493
commit 94bcf6d87f
7 changed files with 38 additions and 50 deletions
@@ -16,7 +16,7 @@ FILE: complexTypeUnwrapping.kt
public get(): R|RE|
}
public final fun foo(x: R|ElementOrRef<*, *>|): R|AbstractElement<out AbstractElement<out AbstractElement<out AbstractElement<out AbstractElement<out kotlin/Any?, out kotlin/Any?>, out AbstractField<kotlin/Any?>>, out AbstractField<AbstractField<kotlin/Any?>>>, out AbstractField<AbstractField<AbstractField<kotlin/Any?>>>>, out AbstractField<AbstractField<AbstractField<AbstractField<kotlin/Any?>>>>>| {
public final fun foo(x: R|ElementOrRef<*, *>|): R|AbstractElement<*, out AbstractField<*>>| {
^foo R|<local>/x|.R|SubstitutionOverride</ElementOrRef.element: R|CapturedType(*)|>|
}
public abstract interface FieldOrRef<FF : R|AbstractField<FF>|> : R|kotlin/Any| {
@@ -24,6 +24,6 @@ FILE: complexTypeUnwrapping.kt
public get(): R|FF|
}
public final fun bar(y: R|FieldOrRef<*>|): R|AbstractField<AbstractField<AbstractField<AbstractField<AbstractField<kotlin/Any?>>>>>| {
public final fun bar(y: R|FieldOrRef<*>|): R|AbstractField<*>| {
^bar R|<local>/y|.R|SubstitutionOverride</FieldOrRef.field: R|CapturedType(*)|>|
}
@@ -37,9 +37,9 @@ fun javaInterfaceTest(a: A1<*>, b: B1<*>, c: C1<*>) {
val x = a.foo()
<!DEBUG_INFO_EXPRESSION_TYPE("A1<*>")!>x<!>
val y = b.foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I<out I<out I<out I<out I<out kotlin.Any?>>>>>")!>y<!>
<!DEBUG_INFO_EXPRESSION_TYPE("I<*>")!>y<!>
val z = c.foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I<out I<out I<out I<out I<out kotlin.Any?>>>>>..I<out I<out I<out I<out I<out kotlin.Any?>>>>>?!")!>z<!>
<!DEBUG_INFO_EXPRESSION_TYPE("I<*>..I<*>?!")!>z<!>
}
fun javaClassTest(a: A2<*>, b: B2<*>, c: C2<*>, d: D2<*>) {
@@ -1,39 +0,0 @@
// ISSUE: KT-59012
// !LANGUAGE: +TypeInferenceOnCallsWithSelfTypes
interface I1<G : I1<G>> {
fun <T : G> foo() : T
fun <T : G> bar(vararg args: T)
}
interface I2<in G : I2<G>> {
fun <T : G> foo() : T
fun <T : G> bar(vararg args: T)
}
interface I3<G> where G : I3<G> {
fun <T> foo() : T where T : G
fun <T> bar(vararg args: T) where T : G
}
fun test(a: I2<*>, b: I3<*>) {
val x = a.foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I2<kotlin.Nothing>")!>x<!>
a.bar()
val y = b.foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I3<*>")!>y<!>
b.bar()
}
fun withTest(a: I1<*>, b: I3<*>) {
with(a) {
val x = foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I1<*>")!>x<!>
bar()
}
with(b) {
val y = foo()
<!DEBUG_INFO_EXPRESSION_TYPE("I3<*>")!>y<!>
bar()
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// ISSUE: KT-59012
// !LANGUAGE: +TypeInferenceOnCallsWithSelfTypes