diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt index f569d4857f4..2d498d16188 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/resolve/substitution/Substitutors.kt @@ -61,7 +61,7 @@ abstract class AbstractConeSubstitutor(private val typeContext: ConeTypeContext) if (it.lowerBound == it.upperBound) it.lowerBound else it } - is ConeCapturedType -> return null + is ConeCapturedType -> return substituteCapturedType() is ConeDefinitelyNotNullType -> this.substituteOriginal() is ConeIntersectionType -> this.substituteIntersectedTypes() is ConeStubType -> return null @@ -69,6 +69,24 @@ abstract class AbstractConeSubstitutor(private val typeContext: ConeTypeContext) } } + private fun ConeCapturedType.substituteCapturedType(): ConeCapturedType? { + val innerType = this.lowerType ?: this.constructor.projection.type + val substitutedInnerType = substituteOrNull(innerType) ?: return null + if (substitutedInnerType is ConeCapturedType) return substitutedInnerType + val substitutedSuperTypes = + this.constructor.supertypes?.map { substituteOrSelf(it) } + + return ConeCapturedType( + captureStatus, + constructor = ConeCapturedTypeConstructor( + wrapProjection(constructor.projection, substitutedInnerType), + substitutedSuperTypes, + typeParameterMarker = constructor.typeParameterMarker + ), + lowerType = if (lowerType != null) substitutedInnerType else null + ) + } + private fun ConeIntersectionType.substituteIntersectedTypes(): ConeIntersectionType? { val substitutedTypes = ArrayList(intersectedTypes.size) var somethingIsSubstituted = false diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.fir.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.fir.kt deleted file mode 100644 index 7c70827d81c..00000000000 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.fir.kt +++ /dev/null @@ -1,12 +0,0 @@ -// !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS -UNUSED_PARAMETER - -class Foo - -fun getFoo(value: T) = null as Foo> -fun takeLambda(block: () -> R) {} - -fun main(x: Int) { - takeLambda { - getFoo(x) - } -} diff --git a/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.kt b/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.kt index 3a60cb6c7e9..5a0f12a76c1 100644 --- a/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.kt +++ b/compiler/testData/diagnostics/tests/inference/capturedTypes/substituteCapturedTypesWithTypeVariables.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -CAST_NEVER_SUCCEEDS -UNUSED_PARAMETER class Foo diff --git a/compiler/testData/diagnostics/tests/inference/commonSystem/cstWithTypeContainingNonFixedVariable.fir.kt b/compiler/testData/diagnostics/tests/inference/commonSystem/cstWithTypeContainingNonFixedVariable.fir.kt index fe7e2881fc8..4c23598c1e7 100644 --- a/compiler/testData/diagnostics/tests/inference/commonSystem/cstWithTypeContainingNonFixedVariable.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/commonSystem/cstWithTypeContainingNonFixedVariable.fir.kt @@ -82,7 +82,7 @@ fun testCapturedVariable() { c2 - val c3 = select(SubInv(), createInvIn()) + val c3 = select(SubInv(), createInvIn()) c3 } diff --git a/compiler/testData/diagnostics/tests/inference/completion/basic.fir.kt b/compiler/testData/diagnostics/tests/inference/completion/basic.fir.kt index 3fd3d3be002..9a6e6faf539 100644 --- a/compiler/testData/diagnostics/tests/inference/completion/basic.fir.kt +++ b/compiler/testData/diagnostics/tests/inference/completion/basic.fir.kt @@ -57,7 +57,7 @@ fun test7(cls: Cls) { } fun test8(cls: Cls) { - id( + id( wrapIn(cls) ) } diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorCrazyProjections.fir.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorCrazyProjections.fir.kt index 69efab98b49..1556957f654 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorCrazyProjections.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorCrazyProjections.fir.kt @@ -12,4 +12,4 @@ fun listOf(): List = null!! // since it has 'out' type projection in 'in' position. val test1 = BOutIn(listOf(), null!!) -val test2 = BInIn(listOf(), null!!) +val test2 = BInIn(listOf(), null!!)