From 2b2f9b3386f186893aa4edee0b6efee508e291cf Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 25 Jun 2020 17:40:56 +0300 Subject: [PATCH] [FIR] Remove delegate specific methods from abstract inference session --- .../AbstractManyCandidatesInferenceSession.kt | 51 +------------------ .../inference/FirBuilderInferenceSession.kt | 3 +- .../fir/resolve/inference/FirCallCompleter.kt | 12 +---- .../FirDelegatedPropertyInferenceSession.kt | 42 +++++++++++++-- 4 files changed, 43 insertions(+), 65 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/AbstractManyCandidatesInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/AbstractManyCandidatesInferenceSession.kt index 50000ecb273..9bce9eb59b6 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/AbstractManyCandidatesInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/AbstractManyCandidatesInferenceSession.kt @@ -10,24 +10,15 @@ import org.jetbrains.kotlin.fir.expressions.FirStatement import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.resolve.calls.candidate -import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor -import org.jetbrains.kotlin.fir.types.ConeKotlinType -import org.jetbrains.kotlin.fir.types.coneTypeUnsafe -import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem -import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor -import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage abstract class AbstractManyCandidatesInferenceSession( - protected val components: BodyResolveComponents, - private val postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer, + protected val components: BodyResolveComponents ) : FirInferenceSession() { private val errorCalls: MutableList = mutableListOf() protected val partiallyResolvedCalls: MutableList> = mutableListOf() private val completedCalls: MutableSet = mutableSetOf() - private val unitType: ConeKotlinType = components.session.builtinTypes.unitType.coneTypeUnsafe() - override val currentConstraintSystem: ConstraintStorage get() = partiallyResolvedCalls.lastOrNull() ?.second @@ -35,8 +26,6 @@ abstract class AbstractManyCandidatesInferenceSession( ?.currentStorage() ?: ConstraintStorage.Empty - private lateinit var resultingConstraintSystem: NewConstraintSystem - override fun addCompetedCall(call: T, candidate: Candidate) where T : FirResolvable, T : FirStatement { // do nothing } @@ -53,44 +42,6 @@ abstract class AbstractManyCandidatesInferenceSession( return !completedCalls.add(call) } - protected open fun prepareForCompletion( - commonSystem: NewConstraintSystem, - partiallyResolvedCalls: List - ) { - // do nothing - } - - fun completeCandidates(): List { - @Suppress("UNCHECKED_CAST") - val resolvedCalls = partiallyResolvedCalls.map { it.first } - val commonSystem = components.inferenceComponents.createConstraintSystem().apply { - addOtherSystem(currentConstraintSystem) - } - prepareForCompletion(commonSystem, resolvedCalls) - components.inferenceComponents.withInferenceSession(DEFAULT) { - @Suppress("UNCHECKED_CAST") - components.callCompleter.completer.complete( - commonSystem.asConstraintSystemCompleterContext(), - KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL, - resolvedCalls as List, - unitType - ) { - postponedArgumentsAnalyzer.analyze( - commonSystem.asPostponedArgumentsAnalyzerContext(), - it, - resolvedCalls.first().candidate - ) - } - } - resultingConstraintSystem = commonSystem - return resolvedCalls - } - protected val FirResolvable.candidate: Candidate get() = candidate()!! - - fun createFinalSubstitutor(): ConeSubstitutor { - return resultingConstraintSystem.asReadOnlyStorage() - .buildAbstractResultingSubstitutor(components.inferenceComponents.ctx) as ConeSubstitutor - } } \ No newline at end of file diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt index 7a23b60456a..c9479a4f586 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirBuilderInferenceSession.kt @@ -26,9 +26,8 @@ import org.jetbrains.kotlin.types.model.TypeConstructorMarker class FirBuilderInferenceSession( components: BodyResolveComponents, - postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer, private val stubsForPostponedVariables: Map, -) : AbstractManyCandidatesInferenceSession(components, postponedArgumentsAnalyzer) { +) : AbstractManyCandidatesInferenceSession(components) { private val commonCalls: MutableList> = mutableListOf() override fun shouldRunCompletion(call: T): Boolean where T : FirResolvable, T : FirStatement { diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt index a1785231b8d..a62bb8898d4 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirCallCompleter.kt @@ -124,18 +124,10 @@ class FirCallCompleter( return PostponedArgumentsAnalyzer( lambdaAnalyzer, inferenceComponents, transformer.components.callResolver - ).also { - lambdaAnalyzer.initAnalyzer(it) - } + ) } private inner class LambdaAnalyzerImpl : LambdaAnalyzer { - private lateinit var postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer - - fun initAnalyzer(postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer) { - this.postponedArgumentsAnalyzer = postponedArgumentsAnalyzer - } - override fun analyzeAndGetLambdaReturnArguments( lambdaAtom: ResolvedLambdaAtom, receiverType: ConeKotlinType?, @@ -185,7 +177,7 @@ class FirCallCompleter( val builderInferenceSession = runIf(stubsForPostponedVariables.isNotEmpty()) { @Suppress("UNCHECKED_CAST") - FirBuilderInferenceSession(components, postponedArgumentsAnalyzer, stubsForPostponedVariables as Map) + FirBuilderInferenceSession(components, stubsForPostponedVariables as Map) } val localContext = towerDataContextForAnonymousFunctions.getValue(lambdaArgument.symbol) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt index e8744fca07c..d804f18373e 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/FirDelegatedPropertyInferenceSession.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents import org.jetbrains.kotlin.fir.resolve.calls.Candidate import org.jetbrains.kotlin.fir.resolve.calls.FirNamedReferenceWithCandidate import org.jetbrains.kotlin.fir.resolve.defaultType +import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeTypeVariableTypeConstructor @@ -24,6 +25,8 @@ import org.jetbrains.kotlin.fir.types.coneTypeSafe import org.jetbrains.kotlin.fir.types.coneTypeUnsafe import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem +import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor +import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition import org.jetbrains.kotlin.util.OperatorNameConventions @@ -33,8 +36,8 @@ class FirDelegatedPropertyInferenceSession( val property: FirProperty, initialCall: FirExpression, components: BodyResolveComponents, - postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer, -) : AbstractManyCandidatesInferenceSession(components, postponedArgumentsAnalyzer) { + private val postponedArgumentsAnalyzer: PostponedArgumentsAnalyzer, +) : AbstractManyCandidatesInferenceSession(components) { init { val initialCandidate = (initialCall as? FirResolvable) ?.calleeReference @@ -46,6 +49,8 @@ class FirDelegatedPropertyInferenceSession( } val expectedType: ConeKotlinType? by lazy { property.returnTypeRef.coneTypeSafe() } + private val unitType: ConeKotlinType = components.session.builtinTypes.unitType.coneTypeUnsafe() + private lateinit var resultingConstraintSystem: NewConstraintSystem override fun shouldRunCompletion(call: T): Boolean where T : FirResolvable, T : FirStatement = false @@ -56,7 +61,33 @@ class FirDelegatedPropertyInferenceSession( override fun shouldCompleteResolvedSubAtomsOf(call: T): Boolean where T : FirResolvable, T : FirStatement = true - override fun prepareForCompletion(commonSystem: NewConstraintSystem, partiallyResolvedCalls: List) { + fun completeCandidates(): List { + @Suppress("UNCHECKED_CAST") + val resolvedCalls = partiallyResolvedCalls.map { it.first } + val commonSystem = components.inferenceComponents.createConstraintSystem().apply { + addOtherSystem(currentConstraintSystem) + } + prepareForCompletion(commonSystem, resolvedCalls) + components.inferenceComponents.withInferenceSession(DEFAULT) { + @Suppress("UNCHECKED_CAST") + components.callCompleter.completer.complete( + commonSystem.asConstraintSystemCompleterContext(), + KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL, + resolvedCalls as List, + unitType + ) { + postponedArgumentsAnalyzer.analyze( + commonSystem.asPostponedArgumentsAnalyzerContext(), + it, + resolvedCalls.first().candidate + ) + } + } + resultingConstraintSystem = commonSystem + return resolvedCalls + } + + private fun prepareForCompletion(commonSystem: NewConstraintSystem, partiallyResolvedCalls: List) { val csBuilder = commonSystem.getBuilder() for (call in partiallyResolvedCalls) { val candidate = call.candidate @@ -67,6 +98,11 @@ class FirDelegatedPropertyInferenceSession( } } + fun createFinalSubstitutor(): ConeSubstitutor { + return resultingConstraintSystem.asReadOnlyStorage() + .buildAbstractResultingSubstitutor(components.inferenceComponents.ctx) as ConeSubstitutor + } + private fun Candidate.addConstraintsForGetValueMethod(commonSystem: ConstraintSystemBuilder) { if (expectedType != null) { val accessor = symbol.fir as? FirSimpleFunction ?: return