FE: don't approximate type variable based types for public/local declarations

#KT-58618 Fixed
This commit is contained in:
Mikhail Glukhikh
2023-05-23 14:43:41 +02:00
committed by Space Team
parent 2cfa3d7e0d
commit 6e58ba8f33
5 changed files with 41 additions and 34 deletions
@@ -375,7 +375,7 @@ abstract class AbstractTypeApproximator(
}
if (typeConstructor is TypeVariableTypeConstructorMarker) {
return if (conf.typeVariable(typeConstructor)) null else type.defaultResult(toSuper)
return if (shouldHandleAsTypeVariable(conf, typeConstructor)) null else type.defaultResult(toSuper)
}
if (typeConstructor.isIntegerLiteralConstantTypeConstructor()) {
@@ -393,6 +393,15 @@ abstract class AbstractTypeApproximator(
return approximateLocalTypes(type, conf, toSuper, depth) // simple classifier type
}
private fun shouldHandleAsTypeVariable(
conf: TypeApproximatorConfiguration,
typeConstructorMarker: TypeVariableTypeConstructorMarker,
): Boolean = when (conf.typeVariable(typeConstructorMarker)) {
TypeApproximatorConfiguration.HandleAsTypeVariable.ALWAYS -> true
TypeApproximatorConfiguration.HandleAsTypeVariable.K2_ONLY -> isK2
TypeApproximatorConfiguration.HandleAsTypeVariable.NEVER -> false
}
private fun approximateDefinitelyNotNullType(
type: DefinitelyNotNullTypeMarker,
conf: TypeApproximatorConfiguration,
@@ -38,7 +38,13 @@ open class TypeApproximatorConfiguration {
*/
open val anonymous = false
open val typeVariable: (TypeVariableTypeConstructorMarker) -> Boolean = { false }
internal enum class HandleAsTypeVariable {
ALWAYS,
K2_ONLY,
NEVER
}
internal open val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable = { HandleAsTypeVariable.NEVER }
open fun capturedType(ctx: TypeSystemInferenceExtensionContext, type: CapturedTypeMarker): Boolean =
true // false means that this type we can leave as is
@@ -56,6 +62,8 @@ open class TypeApproximatorConfiguration {
override val errorType: Boolean get() = true
override val integerLiteralConstantType: Boolean get() = true
override val intersectionTypesInContravariantPositions: Boolean get() = true
override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.K2_ONLY }
}
open class PublicDeclaration(override val localTypes: Boolean, override val anonymous: Boolean) : AllFlexibleSameValue() {
@@ -65,6 +73,8 @@ open class TypeApproximatorConfiguration {
override val integerLiteralConstantType: Boolean get() = true
override val intersectionTypesInContravariantPositions: Boolean get() = true
override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.K2_ONLY }
object SaveAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = false)
object ApproximateAnonymousTypes : PublicDeclaration(localTypes = false, anonymous = true)
}
@@ -79,7 +89,7 @@ open class TypeApproximatorConfiguration {
approximatedCapturedStatus != null && type.captureStatus(ctx) == approximatedCapturedStatus
override val intersection: IntersectionStrategy get() = IntersectionStrategy.ALLOWED
override val typeVariable: (TypeVariableTypeConstructorMarker) -> Boolean get() = { true }
override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.ALWAYS }
}
object IncorporationConfiguration : AbstractCapturedTypesApproximation(CaptureStatus.FOR_INCORPORATION)
@@ -108,7 +118,7 @@ open class TypeApproximatorConfiguration {
override val integerLiteralConstantType: Boolean get() = true
override val allFlexible: Boolean get() = true
override val intersection: IntersectionStrategy get() = IntersectionStrategy.ALLOWED
override val typeVariable: (TypeVariableTypeConstructorMarker) -> Boolean get() = { true }
override val typeVariable: (TypeVariableTypeConstructorMarker) -> HandleAsTypeVariable get() = { HandleAsTypeVariable.ALWAYS }
override val errorType: Boolean get() = true
override fun capturedType(ctx: TypeSystemInferenceExtensionContext, type: CapturedTypeMarker): Boolean = false
@@ -1,13 +0,0 @@
// FIR_DUMP
// WITH_REFLECT
import kotlin.reflect.KProperty
// Definitions
class State<T>(var value: T)
<!NOTHING_TO_INLINE!>inline<!> operator fun <T> State<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value
inline fun <T> remember(block: () -> T): T = block()
// list should have a type of List<Int>, not Any?
val list by remember { State(listOf(0)) }
val first = list.<!UNRESOLVED_REFERENCE!>first<!>()
@@ -1,26 +1,26 @@
FILE: noInferenceFromWrappedDelegate.fir.kt
public final class State<T> : R|kotlin/Any| {
public constructor<T>(value: R|T|): R|State<T>| {
FILE: noInferenceFromWrappedDelegate.kt
public final class State<S> : R|kotlin/Any| {
public constructor<S>(value: R|S|): R|State<S>| {
super<R|kotlin/Any|>()
}
public final var value: R|T| = R|<local>/value|
public get(): R|T|
public set(value: R|T|): R|kotlin/Unit|
public final var value: R|S| = R|<local>/value|
public get(): R|S|
public set(value: R|S|): R|kotlin/Unit|
}
public final inline operator fun <T> R|State<T>|.getValue(thisRef: R|kotlin/Any?|, property: R|kotlin/reflect/KProperty<*>|): R|T| {
^getValue this@R|/getValue|.R|SubstitutionOverride</State.value: R|T|>|
public final inline operator fun <V> R|State<V>|.getValue(thisRef: R|kotlin/Any?|, property: R|kotlin/reflect/KProperty<*>|): R|V| {
^getValue this@R|/getValue|.R|SubstitutionOverride</State.value: R|V|>|
}
public final inline fun <T> remember(block: R|() -> T|): R|T| {
^remember R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|T|>|()
public final inline fun <M> remember(block: R|() -> M|): R|M| {
^remember R|<local>/block|.R|SubstitutionOverride<kotlin/Function0.invoke: R|M|>|()
}
public final val list: R|kotlin/Any?|by R|/remember|<R|State<kotlin/collections/List<kotlin/Int>>|>(<L> = remember@fun <anonymous>(): R|State<kotlin/collections/List<kotlin/Int>>| <inline=Inline, kind=UNKNOWN> {
public final val list: R|kotlin/collections/List<kotlin/Int>|by R|/remember|<R|State<kotlin/collections/List<kotlin/Int>>|>(<L> = remember@fun <anonymous>(): R|State<kotlin/collections/List<kotlin/Int>>| <inline=Inline, kind=UNKNOWN> {
^ R|/State.State|<R|kotlin/collections/List<kotlin/Int>|>(R|kotlin/collections/listOf|<R|kotlin/Int|>(Int(0)))
}
)
public get(): R|kotlin/Any?| {
public get(): R|kotlin/collections/List<kotlin/Int>| {
^ D|/list|.R|/getValue|<R|kotlin/collections/List<kotlin/Int>|>(Null(null), ::R|/list|)
}
public final val first: <ERROR TYPE REF: Unresolved name: first> = R|/list|.<Unresolved name: first>#()
public get(): <ERROR TYPE REF: Unresolved name: first>
public final val first: R|kotlin/Int| = R|/list|.R|kotlin/collections/first|<R|kotlin/Int|>()
public get(): R|kotlin/Int|
@@ -1,12 +1,13 @@
// FIR_IDENTICAL
// FIR_DUMP
// WITH_REFLECT
import kotlin.reflect.KProperty
// Definitions
class State<T>(var value: T)
<!NOTHING_TO_INLINE!>inline<!> operator fun <T> State<T>.getValue(thisRef: Any?, property: KProperty<*>): T = value
inline fun <T> remember(block: () -> T): T = block()
class State<S>(var value: S)
<!NOTHING_TO_INLINE!>inline<!> operator fun <V> State<V>.getValue(thisRef: Any?, property: KProperty<*>): V = value
inline fun <M> remember(block: () -> M): M = block()
// list should have a type of List<Int>, not Any?
val list by remember { State(listOf(0)) }