From ec055eb4185371a05a9bf0bd72dbc3aeb1be0f61 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 22 Jul 2022 15:13:39 +0200 Subject: [PATCH] K1: cleanup new inference constructor resolve in CallResolverUtil --- .../resolve/calls/util/CallResolverUtil.kt | 59 +++++++++---------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt index 516038ea2a6..b2b2833272b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/util/CallResolverUtil.kt @@ -52,6 +52,7 @@ import org.jetbrains.kotlin.types.expressions.OperatorConventions import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.util.buildNotFixedVariablesToPossibleResultType import org.jetbrains.kotlin.utils.SmartList +import org.jetbrains.kotlin.utils.addToStdlib.runIf enum class ResolveArgumentsMode { RESOLVE_FUNCTION_ARGUMENTS, @@ -283,29 +284,30 @@ fun isArrayOrArrayLiteral(argument: ValueArgument, trace: BindingTrace): Boolean return KotlinBuiltIns.isArrayOrPrimitiveArray(type) } -private fun computeConstructorDescriptorsToResolveAndReceiver( - constructors: Collection, +private fun computeConstructorDispatchReceiver( containingClass: ClassDescriptor, scope: LexicalScope, - substitutor: TypeSubstitutor?, - syntheticScopes: SyntheticScopes -): Pair, ReceiverValue?>? { - val dispatchReceiver: ReceiverValue? = if (containingClass.isInner) { + substitutor: TypeSubstitutor? +): ReceiverValue? { + return runIf(containingClass.isInner) { val outerClassType = (containingClass.containingDeclaration as? ClassDescriptor)?.defaultType ?: return null val substitutedOuterClassType = substitutor?.substitute(outerClassType, Variance.INVARIANT) ?: outerClassType - val receiver = scope.getImplicitReceiversHierarchy().firstOrNull { + scope.getImplicitReceiversHierarchy().firstOrNull { KotlinTypeChecker.DEFAULT.isSubtypeOf(it.type, substitutedOuterClassType) - } ?: return null - - receiver.value - } else { - null + }?.value } +} - val syntheticConstructors = constructors.flatMap { syntheticScopes.collectSyntheticConstructors(it) } - - return constructors + syntheticConstructors to dispatchReceiver - +private fun computeConstructorDescriptorsToResolve( + containingClass: ClassDescriptor, + typeAliasDescriptorIfAny: TypeAliasDescriptor?, + syntheticScopes: SyntheticScopes +): Collection { + val simpleConstructors = + typeAliasDescriptorIfAny?.constructors?.mapNotNull(TypeAliasConstructorDescriptor::withDispatchReceiver) + ?: containingClass.constructors + val syntheticConstructors = simpleConstructors.flatMap { syntheticScopes.collectSyntheticConstructors(it) } + return simpleConstructors + syntheticConstructors } fun resolveConstructorCallWithGivenDescriptors( @@ -320,25 +322,20 @@ fun resolveConstructorCallWithGivenDescriptors( @Suppress("NAME_SHADOWING") val constructorType = constructorType.unwrap() - val knownSubstitutor = if (useKnownTypeSubstitutor) { - TypeSubstitutor.create((constructorType as? AbbreviatedType)?.abbreviation ?: constructorType) - } else null - val typeAliasDescriptor = if (constructorType is AbbreviatedType) { - constructorType.abbreviation.constructor.declarationDescriptor as? TypeAliasDescriptor - } else null + val constructorTypeAbbreviation = (constructorType as? AbbreviatedType)?.abbreviation + val knownSubstitutor = runIf(useKnownTypeSubstitutor) { + TypeSubstitutor.create(constructorTypeAbbreviation ?: constructorType) + } + val typeAliasDescriptor = constructorTypeAbbreviation?.constructor?.declarationDescriptor as? TypeAliasDescriptor - val (constructors, receiver) = computeConstructorDescriptorsToResolveAndReceiver( - constructors = typeAliasDescriptor?.constructors?.mapNotNull(TypeAliasConstructorDescriptor::withDispatchReceiver) - ?: containingClass.constructors, - containingClass, - context.scope, - knownSubstitutor, - syntheticScopes - ) ?: (emptyList() to null) + val receiver = computeConstructorDispatchReceiver(containingClass, context.scope, knownSubstitutor) + val allConstructors = runIf(!containingClass.isInner || receiver != null) { + computeConstructorDescriptorsToResolve(containingClass, typeAliasDescriptor, syntheticScopes) + }.orEmpty() val resolutionResults = PSICallResolver.runResolutionAndInferenceForGivenDescriptors( context, - constructors, + allConstructors, tracingStrategy, KotlinCallKind.FUNCTION, knownSubstitutor,