diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt index ade5bba97db..c8e9ac4de75 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt @@ -5,9 +5,11 @@ package org.jetbrains.kotlin.fir.resolve.substitution +import org.jetbrains.kotlin.fir.resolve.inference.inferenceComponents import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl +import org.jetbrains.kotlin.types.TypeApproximatorConfiguration abstract class AbstractConeSubstitutor : ConeSubstitutor() { private fun wrapProjection(old: ConeTypeProjection, newType: ConeKotlinType): ConeTypeProjection { @@ -145,6 +147,13 @@ fun ConeSubstitutor.chain(other: ConeSubstitutor): ConeSubstitutor { data class ConeSubstitutorByMap(val substitution: Map) : AbstractConeSubstitutor() { override fun substituteType(type: ConeKotlinType): ConeKotlinType? { if (type !is ConeTypeParameterType) return null - return substitution[type.lookupTag.symbol].updateNullabilityIfNeeded(type) + val result = substitution[type.lookupTag.symbol].updateNullabilityIfNeeded(type) ?: return null + val session = type.lookupTag.symbol.fir.session + if (type.isUnsafeVarianceType(session)) { + return session.inferenceComponents.approximator.approximateToSuperType( + result, TypeApproximatorConfiguration.SubtypeCapturedTypesApproximation + ) as? ConeKotlinType ?: result + } + return result } } diff --git a/compiler/testData/diagnostics/tests/generics/projectionsScope/unsafeVarianceWithRecursiveGenerics.fir.kt b/compiler/testData/diagnostics/tests/generics/projectionsScope/unsafeVarianceWithRecursiveGenerics.fir.kt index e13533974c9..ebdcf4738bc 100644 --- a/compiler/testData/diagnostics/tests/generics/projectionsScope/unsafeVarianceWithRecursiveGenerics.fir.kt +++ b/compiler/testData/diagnostics/tests/generics/projectionsScope/unsafeVarianceWithRecursiveGenerics.fir.kt @@ -6,6 +6,6 @@ internal fun Any.matchesRendering(other: Any): Boolean { return when { this::class != other::class -> false this !is UpdatableRendering<*> -> true - else -> this.canUpdateFrom(other as UpdatableRendering<*>) + else -> this.canUpdateFrom(other as UpdatableRendering<*>) } }