From a509e819b59c38b07469fb31fa7e6cf78e581bca Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Mon, 20 Sep 2021 20:46:07 +0300 Subject: [PATCH] [FIR] Revert changes related to reporting WRONG_NUMBER_OF_TYPE_ARGUMENTS for type aliases --- .../providers/impl/FirTypeResolverImpl.kt | 49 ++----------------- .../typealias/innerTypeAliasAsType.fir.kt | 4 +- .../typealias/innerTypeAliasAsType2.fir.kt | 24 --------- .../tests/typealias/innerTypeAliasAsType2.kt | 1 + ...onstructorInferenceInSupertypesList.fir.kt | 7 +++ ...iasConstructorInferenceInSupertypesList.kt | 1 - 6 files changed, 15 insertions(+), 71 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.fir.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.fir.kt diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt index 9a4bf479812..e88823d0994 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/providers/impl/FirTypeResolverImpl.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic import org.jetbrains.kotlin.fir.diagnostics.ConeUnexpectedTypeArgumentsError import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind import org.jetbrains.kotlin.fir.resolve.* -import org.jetbrains.kotlin.fir.resolve.calls.fullyExpandedClass import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeOuterClassArgumentsRequired import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedQualifierError import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnsupportedDynamicType @@ -235,12 +234,12 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { } } - if (symbol is FirClassLikeSymbol<*>) { + if (symbol is FirRegularClassSymbol) { val isPossibleBareType = areBareTypesAllowed && allTypeArguments.isEmpty() if (!isPossibleBareType) { val actualSubstitutor = substitutor ?: ConeSubstitutor.Empty - val originalTypeParameters = collectTypeParameters(symbol) + val originalTypeParameters = symbol.fir.typeParameters val (typeParametersAlignedToQualifierParts, outerDeclarations) = getClassesAlignedToQualifierParts( symbol, @@ -252,7 +251,7 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { when (symbol) { is FirTypeAliasSymbol -> outerDeclarations.sumOf { it?.let { d -> getActualTypeParametersCount(d) } ?: 0 } - else -> (symbol as FirClassSymbol<*>).typeParameterSymbols.size + else -> symbol.typeParameterSymbols.size } for ((typeParameterIndex, typeParameter) in originalTypeParameters.withIndex()) { @@ -322,45 +321,6 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { } } - private fun collectTypeParameters(symbol: FirClassLikeSymbol<*>): List { - if (symbol is FirClassSymbol<*>) { - return symbol.fir.typeParameters - } - - require(symbol is FirTypeAliasSymbol) - - val typeAliasFir = symbol.fir - val typeAliasTypeParameters = typeAliasFir.typeParameters.toMutableList() - val fullyExpandedClass = typeAliasFir.fullyExpandedClass(session) - val newTypeParameters = if (fullyExpandedClass != null) { // TODO: Should not be null, move to resolver? - val expandedTypeRef = typeAliasFir.expandedTypeRef - - fun checkTypeArguments(typeArgument: ConeTypeProjection) { - when (typeArgument) { - is ConeTypeParameterType -> typeAliasTypeParameters.removeIf { it.symbol == typeArgument.lookupTag.symbol } - is ConeClassLikeType -> { - for (subTypeArgument in typeArgument.typeArguments) { - checkTypeArguments(subTypeArgument) - } - } - is ConeKotlinTypeProjection -> checkTypeArguments(typeArgument.type) - else -> { - } - } - } - - checkTypeArguments(expandedTypeRef.coneType) - fullyExpandedClass.typeParameters.toMutableList() - } else { - mutableListOf() - } - - for (typeParameter in typeAliasTypeParameters) { - newTypeParameters.add(typeParameter) - } - return newTypeParameters - } - @OptIn(SymbolInternals::class) private fun getClassesAlignedToQualifierParts( symbol: FirClassLikeSymbol<*>, @@ -393,7 +353,8 @@ class FirTypeResolverImpl(private val session: FirSession) : FirTypeResolver() { currentClassLikeDeclaration = reversedOuterClasses[index] val typeParameters = when (currentClassLikeDeclaration) { is FirTypeAlias -> currentClassLikeDeclaration.typeParameters - else -> (currentClassLikeDeclaration as? FirClass)?.typeParameters + is FirClass -> currentClassLikeDeclaration.typeParameters + else -> null } if (currentClassLikeDeclaration != null && typeParameters != null) { for (typeParameter in typeParameters) { diff --git a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType.fir.kt b/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType.fir.kt index b41536cc81e..cc100a53247 100644 --- a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType.fir.kt @@ -26,9 +26,9 @@ fun test5(x: Outer.GenericNestedAlias) = x fun test6(x: Outer.GenericNestedAlias) = x fun test7(x: Outer.GenericNestedAlias) = x fun test8(x: Outer.GenericNestedAlias) = x -fun test9(x: Outer.InnerAlias) = x +fun test9(x: Outer.InnerAlias) = x fun test10(x: Outer.InnerAlias) = x fun test11(x: Outer.InnerAlias) = x -fun test12(x: Outer.GenericInnerAlias) = x +fun test12(x: Outer.GenericInnerAlias) = x fun test13(x: Outer.GenericInnerAlias) = x fun test14(x: Outer.GenericInnerAlias) = x diff --git a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.fir.kt b/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.fir.kt deleted file mode 100644 index b891a7ddb0b..00000000000 --- a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.fir.kt +++ /dev/null @@ -1,24 +0,0 @@ -// !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER -TOPLEVEL_TYPEALIASES_ONLY - -class C { - inner class D - - typealias DA = D - typealias SDA = C.D - typealias TSDA = C.D - typealias TC = C - typealias SSDA = C<*>.D - typealias SSC = C<*> -} - -fun test1(x: C.DA) = x -fun test2(x: C.SDA) = x -fun test3(x: C.TSDA) = x -fun test4(x: C.TC) = x - -fun test5(x: C<*>.DA) = x -fun test6(x: C<*>.TSDA) = x -fun test7(x: C<*>.TC) = x - -fun test8(x: C.SSDA) = x -fun test9(x: C.SSC) = x diff --git a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.kt b/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.kt index f1a2018e98d..b395df6be23 100644 --- a/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.kt +++ b/compiler/testData/diagnostics/tests/typealias/innerTypeAliasAsType2.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // !DIAGNOSTICS: -UNUSED_VARIABLE -UNUSED_PARAMETER -TOPLEVEL_TYPEALIASES_ONLY class C { diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.fir.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.fir.kt new file mode 100644 index 00000000000..9633af61203 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.fir.kt @@ -0,0 +1,7 @@ +open class Ref(var x: T) + +typealias R = Ref + +// Type inference SHOULD NOT work for type alias constructor in supertypes list +class Test1 : R(0) +class Test2 : R(0) diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.kt index 1f19a670ebf..955d07623f5 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasConstructorInferenceInSupertypesList.kt @@ -1,4 +1,3 @@ -// FIR_IDENTICAL open class Ref(var x: T) typealias R = Ref