From 3dc0935da4c8fa344a6ff8d1d5240a96fe5c15b8 Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Tue, 28 Sep 2021 00:22:28 +0300 Subject: [PATCH] [FIR] Pass bounds from original type to fully expanded ^KT-40203 Fixed --- ...CreateFreshTypeVariableSubstitutorStage.kt | 32 +++++++++++++++---- ...oundViolationInTypeAliasConstructor.fir.kt | 4 +-- ...asConstructorTypeArgumentsInference.fir.kt | 2 +- ...peArgumentsInferenceWithNestedCalls.fir.kt | 6 ++-- ...erOfArgumentsInTypeAliasConstructor.fir.kt | 8 ++--- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt index 6dee29c18e8..22311a9e2ce 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt @@ -6,17 +6,15 @@ package org.jetbrains.kotlin.fir.resolve.calls import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.declarations.FirResolvePhase -import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRef -import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRefsOwner +import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.renderWithType import org.jetbrains.kotlin.fir.resolve.fullyExpandedType import org.jetbrains.kotlin.fir.resolve.inference.ConeTypeParameterBasedTypeVariable -import org.jetbrains.kotlin.fir.resolve.inference.inferenceComponents import org.jetbrains.kotlin.fir.resolve.inference.model.ConeDeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.fir.resolve.inference.model.ConeExplicitTypeParameterConstraintPosition import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap +import org.jetbrains.kotlin.fir.scopes.impl.toConeType import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag import org.jetbrains.kotlin.fir.symbols.ensureResolved import org.jetbrains.kotlin.fir.typeContext @@ -150,12 +148,34 @@ private fun createToFreshVariableSubstitutorAndAddInitialConstraints( for (index in typeParameters.indices) { val typeParameter = typeParameters[index] val freshVariable = freshTypeVariables[index] - //val position = DeclaredUpperBoundConstraintPosition(typeParameter) - for (upperBound in typeParameter.symbol.fir.bounds) { + val parameterSymbolFromExpandedClass = typeParameter.symbol.fir.getTypeParameterFromExpandedClass(index, session) + + for (upperBound in parameterSymbolFromExpandedClass.bounds) { freshVariable.addSubtypeConstraint(upperBound.coneType/*, position*/) } } return toFreshVariables to freshTypeVariables } + +private fun FirTypeParameter.getTypeParameterFromExpandedClass(index: Int, session: FirSession): FirTypeParameter { + val containingDeclaration = containingDeclarationSymbol?.fir + if (containingDeclaration is FirRegularClass) { + return containingDeclaration.typeParameters.elementAtOrNull(index)?.symbol?.fir ?: this + } else if (containingDeclaration is FirTypeAlias) { + val typeParameterConeType = toConeType() + val expandedConeType = containingDeclaration.expandedTypeRef.coneType + val typeArgumentIndex = expandedConeType.typeArguments.indexOfFirst { it.type == typeParameterConeType } + val expandedTypeFir = expandedConeType.toSymbol(session)?.fir + if (expandedTypeFir is FirTypeParameterRefsOwner) { + val typeParameterFir = expandedTypeFir.typeParameters.elementAtOrNull(typeArgumentIndex)?.symbol?.fir ?: return this + if (expandedTypeFir is FirTypeAlias) { + return typeParameterFir.getTypeParameterFromExpandedClass(typeArgumentIndex, session) + } + return typeParameterFir + } + } + + return this +} diff --git a/compiler/testData/diagnostics/tests/typealias/boundViolationInTypeAliasConstructor.fir.kt b/compiler/testData/diagnostics/tests/typealias/boundViolationInTypeAliasConstructor.fir.kt index c8a6567a2f2..bd83204af87 100644 --- a/compiler/testData/diagnostics/tests/typealias/boundViolationInTypeAliasConstructor.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/boundViolationInTypeAliasConstructor.fir.kt @@ -4,8 +4,8 @@ typealias N = Num typealias N2 = N val x1 = Num<String>("") -val x2 = N<String>("") -val x3 = N2<String>("") +val x2 = N<String>("") +val x3 = N2<String>("") class TColl> diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.fir.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.fir.kt index be9f51d529f..e00bd782068 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInference.fir.kt @@ -4,7 +4,7 @@ class Num(val x: Tn) typealias N = Num val test0 = N(1) -val test1 = N("1") +val test1 = N("1") class Cons(val head: T, val tail: Cons?) diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInferenceWithNestedCalls.fir.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInferenceWithNestedCalls.fir.kt index 0970935e7dd..c21845f5748 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInferenceWithNestedCalls.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorTypeArgumentsInferenceWithNestedCalls.fir.kt @@ -14,6 +14,6 @@ typealias C = Cons typealias C2 = MapLike val test1 = C(1, C(2, null)) -val test2 = C(1, C("", null)) -val test23 = C2(if (true) 1 else null) -val test234 = C2(C2(if (true) 1 else null)) +val test2 = C(1, C("", null)) +val test23 = C2(if (true) 1 else null) +val test234 = C2(C2(if (true) 1 else null)) diff --git a/compiler/testData/diagnostics/tests/typealias/wrongNumberOfArgumentsInTypeAliasConstructor.fir.kt b/compiler/testData/diagnostics/tests/typealias/wrongNumberOfArgumentsInTypeAliasConstructor.fir.kt index 1294e041f74..ace851d2438 100644 --- a/compiler/testData/diagnostics/tests/typealias/wrongNumberOfArgumentsInTypeAliasConstructor.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/wrongNumberOfArgumentsInTypeAliasConstructor.fir.kt @@ -28,14 +28,14 @@ val test3pr = P2(1, "") class Num(val x: T) typealias N = Num -val testN0 = N("") +val testN0 = N("") val testN1 = N(1) -val testN1a = N<String>("") +val testN1a = N<String>("") val testN2 = N(1) class MyPair(val string: T1, val number: T2) typealias MP = MyPair val testMP0 = MP("", 1) -val testMP1 = MP(1, "") -val testMP2 = MP("", "") +val testMP1 = MP(1, "") +val testMP2 = MP("", "")