From 999d1f982e7c93844c19b10af5674367ebb2e052 Mon Sep 17 00:00:00 2001 From: Victor Petukhov Date: Fri, 19 Mar 2021 15:25:19 +0300 Subject: [PATCH] Introduce hierarchy of inference sessions --- .../DelegatedPropertyInferenceSession.kt | 8 +++++-- .../resolve/DelegatedPropertyResolver.kt | 4 ++-- .../inference/BuilderInferenceSession.kt | 22 ++++++++++++++----- .../calls/components/InferenceSession.kt | 4 ++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt index bd9d9347260..44dae2479b7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyInferenceSession.kt @@ -32,7 +32,8 @@ class DelegatedPropertyInferenceSession( postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer, kotlinConstraintSystemCompleter: KotlinConstraintSystemCompleter, callComponents: KotlinCallComponents, - builtIns: KotlinBuiltIns + builtIns: KotlinBuiltIns, + override val parentSession: InferenceSession? ) : ManyCandidatesResolver( psiCallResolver, postponedArgumentsAnalyzer, kotlinConstraintSystemCompleter, callComponents, builtIns ) { @@ -92,7 +93,10 @@ class DelegatedPropertyInferenceSession( override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true } -class InferenceSessionForExistingCandidates(private val resolveReceiverIndependently: Boolean) : InferenceSession { +class InferenceSessionForExistingCandidates( + private val resolveReceiverIndependently: Boolean, + override val parentSession: InferenceSession? +) : InferenceSession { override fun shouldRunCompletion(candidate: KotlinResolutionCandidate): Boolean { return !ErrorUtils.isError(candidate.resolvedCall.candidateDescriptor) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt index 2fd001e7895..ede549cee93 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DelegatedPropertyResolver.kt @@ -580,7 +580,7 @@ class DelegatedPropertyResolver( val contextForProvideDelegate = createContextForProvideDelegateMethod( scopeForDelegate, delegateDataFlow, traceForProvideDelegate, - InferenceSessionForExistingCandidates(substitutionMap != null) + InferenceSessionForExistingCandidates(substitutionMap != null, inferenceSession) ) val provideDelegateResults = getProvideDelegateMethod( @@ -652,7 +652,7 @@ class DelegatedPropertyResolver( val newInferenceSession = DelegatedPropertyInferenceSession( variableDescriptor, expectedType, psiCallResolver, postponedArgumentsAnalyzer, kotlinConstraintSystemCompleter, - callComponents, builtIns + callComponents, builtIns, inferenceSession ) val receiver = createReceiverForGetSetValueMethods(delegateExpression, delegateType, trace) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt index 07c667c318d..c46dc2b42e6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/BuilderInferenceSession.kt @@ -15,10 +15,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.psi.psiUtil.isAncestor import org.jetbrains.kotlin.resolve.* import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver -import org.jetbrains.kotlin.resolve.calls.components.CompletedCallInfo -import org.jetbrains.kotlin.resolve.calls.components.NewConstraintSystemImpl -import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer -import org.jetbrains.kotlin.resolve.calls.components.stableType +import org.jetbrains.kotlin.resolve.calls.components.* import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter @@ -71,6 +68,8 @@ class BuilderInferenceSession( private var hasInapplicableCall = false + override val parentSession = topLevelCallContext.inferenceSession + override fun shouldRunCompletion(candidate: KotlinResolutionCandidate): Boolean { val system = candidate.getSystem() as NewConstraintSystemImpl @@ -147,7 +146,18 @@ class BuilderInferenceSession( descriptor.extensionReceiverParameter?.type?.contains { it is StubType } == true } - private fun isTopLevelBuilderInferenceCall() = topLevelCallContext.inferenceSession !is BuilderInferenceSession + private fun isTopLevelBuilderInferenceCall() = findParentBuildInferenceSession() == null + + private fun findParentBuildInferenceSession(): BuilderInferenceSession? { + var currentSession: InferenceSession? = parentSession + + while (currentSession != null) { + if (currentSession is BuilderInferenceSession) return currentSession + currentSession = currentSession.parentSession + } + + return null + } fun hasInapplicableCall(): Boolean = hasInapplicableCall @@ -234,7 +244,7 @@ class BuilderInferenceSession( bindings[nonFixedType.constructor] = variable.defaultType } - val parentBuilderInferenceCallSession = topLevelCallContext.inferenceSession as? BuilderInferenceSession + val parentBuilderInferenceCallSession = findParentBuildInferenceSession() if (parentBuilderInferenceCallSession != null) { bindings.putAll(parentBuilderInferenceCallSession.createNonFixedTypeToVariableMap()) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt index 61d6eb3c4b5..45327d0cdf6 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/InferenceSession.kt @@ -12,8 +12,12 @@ import org.jetbrains.kotlin.types.TypeConstructor import org.jetbrains.kotlin.types.UnwrappedType interface InferenceSession { + val parentSession: InferenceSession? + companion object { val default = object : InferenceSession { + override val parentSession: InferenceSession? = null + override fun shouldRunCompletion(candidate: KotlinResolutionCandidate): Boolean = true override fun addPartialCallInfo(callInfo: PartialCallInfo) {} override fun addErrorCallInfo(callInfo: ErrorCallInfo) {}