[FIR/NI] Refactor type variable gathering from lambda types

Motivation:
- drop getArguments from type context as a duplicate of getArgumentList
- reduce the number of collection allocations in getAllDeeplyRelatedTypeVariables

Additional minor improvements, test data fixes
This commit is contained in:
Pavel Kirpichenkov
2020-10-05 19:31:52 +03:00
parent ef44077cb7
commit 39a87435ee
14 changed files with 77 additions and 56 deletions
@@ -27,7 +27,7 @@ interface ConstraintSystemUtilContext {
// PostponedArgumentInputTypesResolver
fun <T> createArgumentConstraintPosition(argument: T): ArgumentConstraintPosition<T>
fun <T> createFixVariableConstraintPosition(variable: TypeVariableMarker, atom: T): FixVariableConstraintPosition<T>
fun extractParameterTypesFromDeclaration(declaration: PostponedAtomWithRevisableExpectedType): List<KotlinTypeMarker?>?
fun extractLambdaParameterTypesFromDeclaration(declaration: PostponedAtomWithRevisableExpectedType): List<KotlinTypeMarker?>?
fun PostponedAtomWithRevisableExpectedType.isAnonymousFunction(): Boolean
fun PostponedAtomWithRevisableExpectedType.isFunctionExpressionWithReceiver(): Boolean
fun createTypeVariableForLambdaReturnType(): TypeVariableMarker
@@ -332,7 +332,7 @@ class PostponedArgumentInputTypesResolver(
for (argument in postponedArguments) {
if (argument !is LambdaWithTypeVariableAsExpectedTypeMarker) continue
if (argument.parameterTypesFromDeclaration != null) continue
argument.updateParameterTypesFromDeclaration(extractParameterTypesFromDeclaration(argument))
argument.updateParameterTypesFromDeclaration(extractLambdaParameterTypesFromDeclaration(argument))
}
return postponedArguments.any { argument ->
@@ -359,21 +359,33 @@ class PostponedArgumentInputTypesResolver(
private fun Context.getAllDeeplyRelatedTypeVariables(
type: KotlinTypeMarker,
variableDependencyProvider: TypeVariableDependencyInformationProvider
): List<TypeVariableTypeConstructorMarker> {
variableDependencyProvider: TypeVariableDependencyInformationProvider,
): Collection<TypeVariableTypeConstructorMarker> {
val collectedVariables = mutableSetOf<TypeVariableTypeConstructorMarker>()
getAllDeeplyRelatedTypeVariables(type, variableDependencyProvider, collectedVariables)
return collectedVariables
}
private fun Context.getAllDeeplyRelatedTypeVariables(
type: KotlinTypeMarker,
variableDependencyProvider: TypeVariableDependencyInformationProvider,
typeVariableCollector: MutableSet<TypeVariableTypeConstructorMarker>
) {
val typeConstructor = type.typeConstructor()
return when {
when {
typeConstructor is TypeVariableTypeConstructorMarker -> {
val relatedVariables = variableDependencyProvider.getDeeplyDependentVariables(typeConstructor).orEmpty()
listOf(typeConstructor) + relatedVariables.filterIsInstance<TypeVariableTypeConstructorMarker>()
typeVariableCollector.add(typeConstructor)
typeVariableCollector.addAll(relatedVariables.filterIsInstance<TypeVariableTypeConstructorMarker>())
}
type.argumentsCount() > 0 -> {
type.getArguments().flatMap {
if (it.isStarProjection()) emptyList() else getAllDeeplyRelatedTypeVariables(it.getType(), variableDependencyProvider)
for (typeArgument in type.lowerBoundIfFlexible().asArgumentList()) {
if (!typeArgument.isStarProjection()) {
getAllDeeplyRelatedTypeVariables(typeArgument.getType(), variableDependencyProvider, typeVariableCollector)
}
}
}
else -> emptyList()
}
}