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 7662fa9d242..c9196ff7ae5 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 @@ -438,10 +438,12 @@ class BuilderInferenceSession( integrateConstraints(initialStorage, nonFixedToVariablesSubstitutor, false) for (call in commonCalls) { - integrateConstraints(call.callResolutionResult.constraintSystem, nonFixedToVariablesSubstitutor, false) + val storage = call.callResolutionResult.constraintSystem.getBuilder().currentStorage() + integrateConstraints(storage, nonFixedToVariablesSubstitutor, false) } for (call in partiallyResolvedCallsInfo) { - integrateConstraints(call.callResolutionResult.constraintSystem, nonFixedToVariablesSubstitutor, true) + val storage = call.callResolutionResult.constraintSystem.getBuilder().currentStorage() + integrateConstraints(storage, nonFixedToVariablesSubstitutor, true) } return commonSystem.notFixedTypeVariables.all { it.value.constraints.isEmpty() } @@ -469,7 +471,8 @@ class BuilderInferenceSession( nonFixedTypesToResultSubstitutor: NewTypeSubstitutor, nonFixedTypesToResult: Map ) { - val resultingCallSubstitutor = completedCall.callResolutionResult.constraintSystem.fixedTypeVariables.entries + val storage = completedCall.callResolutionResult.constraintSystem.getBuilder().currentStorage() + val resultingCallSubstitutor = storage.fixedTypeVariables.entries .associate { it.key to nonFixedTypesToResultSubstitutor.safeSubstitute(it.value as UnwrappedType) } // TODO: SUB val resultingSubstitutor = diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt index 464ecce25d1..c7240a5fc5a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinResolutionCallbacksImpl.kt @@ -103,6 +103,9 @@ class KotlinResolutionCallbacksImpl( ) as KotlinType } + override fun createEmptyConstraintSystem(): NewConstraintSystem = + NewConstraintSystemImpl(callComponents.constraintInjector, callComponents.builtIns, callComponents.kotlinTypeRefiner) + override fun resolveCallableReferenceArgument( argument: CallableReferenceKotlinCallArgument, expectedType: UnwrappedType?, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 259fae1b6f4..1e4d5ea9f7b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -107,7 +107,8 @@ class KotlinToResolvedCallTransformer( is CompletedCallResolutionResult, is ErrorCallResolutionResult -> { val candidate = (baseResolvedCall as SingleCallResolutionResult).resultCallAtom - val resultSubstitutor = baseResolvedCall.constraintSystem.buildResultingSubstitutor(typeSystemContext) + val resultSubstitutor = + baseResolvedCall.constraintSystem.getBuilder().currentStorage().buildResultingSubstitutor(typeSystemContext) if (context.inferenceSession.writeOnlyStubs(baseResolvedCall)) { val stub = createStubResolvedCallAndWriteItToTrace( candidate, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index d9d2d255c7d..e559ec97ba7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -541,7 +541,7 @@ class PSICallResolver( val psiKotlinCall = variable.resolvedCall.atom.psiKotlinCall - val variableResult = PartialCallResolutionResult(variable.resolvedCall, listOf(), variable.getSystem().asReadOnlyStorage()) + val variableResult = PartialCallResolutionResult(variable.resolvedCall, listOf(), variable.getSystem()) return SubKotlinCallArgumentImpl( CallMaker.makeExternalValueArgument((variableReceiver.receiverValue as ExpressionReceiver).expression), diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/StubTypesBasedInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/StubTypesBasedInferenceSession.kt index f503145f48c..3f0a7b9ce05 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/StubTypesBasedInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/StubTypesBasedInferenceSession.kt @@ -65,7 +65,8 @@ abstract class StubTypesBasedInferenceSession( } override fun currentConstraintSystem(): ConstraintStorage { - return partiallyResolvedCallsInfo.lastOrNull()?.callResolutionResult?.constraintSystem ?: ConstraintStorage.Empty + return partiallyResolvedCallsInfo.lastOrNull()?.callResolutionResult?.constraintSystem?.getBuilder()?.currentStorage() + ?: ConstraintStorage.Empty } override fun callCompleted(resolvedAtom: ResolvedAtom): Boolean = @@ -116,7 +117,7 @@ abstract class StubTypesBasedInferenceSession( for (callInfo in listOf(goodCandidate, badCandidate)) { val atomsToAnalyze = mutableListOf(callInfo.callResolutionResult) val system = NewConstraintSystemImpl(callComponents.constraintInjector, builtIns, callComponents.kotlinTypeRefiner).apply { - addOtherSystem(callInfo.callResolutionResult.constraintSystem) + addOtherSystem(callInfo.callResolutionResult.constraintSystem.getBuilder().currentStorage()) /* * This is needed for very stupid case, when we have some delegate with good `getValue` and bad `setValue` that * was provided by some function call with generic (e.g. var x by lazy { "" }) @@ -129,7 +130,8 @@ abstract class StubTypesBasedInferenceSession( * stub atoms in order to call completer doesn't fail */ if (callInfo === badCandidate) { - for ((typeVariable, fixedType) in allCandidates[0].resolutionResult.constraintSystem.fixedTypeVariables) { + val storage = allCandidates[0].resolutionResult.constraintSystem.getBuilder().currentStorage() + for ((typeVariable, fixedType) in storage.fixedTypeVariables) { if (typeVariable in this.notFixedTypeVariables) { val type = (typeVariable as TypeConstructor).typeForTypeVariable() addEqualityConstraint( @@ -178,7 +180,7 @@ abstract class StubTypesBasedInferenceSession( commonSystem: NewConstraintSystem ): CallResolutionResult { val diagnostics = diagnosticsHolder.getDiagnostics() + callResolutionResult.diagnostics + commonSystem.errors.asDiagnostics() - return CompletedCallResolutionResult(callResolutionResult.resultCallAtom, diagnostics, commonSystem.asReadOnlyStorage()) + return CompletedCallResolutionResult(callResolutionResult.resultCallAtom, diagnostics, commonSystem) } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt index 93f7763aa1c..7b8d4160060 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ExternalComponents.kt @@ -98,6 +98,8 @@ interface KotlinResolutionCallbacks { fun findResultType(constraintSystem: NewConstraintSystem, typeVariable: TypeVariableTypeConstructor): KotlinType? + fun createEmptyConstraintSystem(): NewConstraintSystem + fun bindStubResolvedCallForCandidate(candidate: ResolvedCallAtom) fun isCompileTimeConstant(resolvedAtom: ResolvedCallAtom, expectedType: UnwrappedType): Boolean diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt index 10320829315..402d47dd025 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/KotlinCallCompleter.kt @@ -228,7 +228,7 @@ class KotlinCallCompleter( CandidateWithDiagnostics(candidate, diagnosticsHolder.getDiagnostics() + candidate.diagnostics) } - return AllCandidatesResolutionResult(completedCandidates) + return AllCandidatesResolutionResult(completedCandidates, resolutionCallbacks.createEmptyConstraintSystem()) } private fun ResolutionCandidate.runCompletion( @@ -350,17 +350,17 @@ class KotlinCallCompleter( diagnosticsHolder: KotlinDiagnosticsHolder.SimpleHolder, forwardToInferenceSession: Boolean = false ): CallResolutionResult { - val systemStorage = getSystem().asReadOnlyStorage() + val constraintSystem = getSystem() val allDiagnostics = diagnosticsHolder.getDiagnostics() + diagnostics if (isErrorCandidate()) { - return ErrorCallResolutionResult(resolvedCall, allDiagnostics, systemStorage) + return ErrorCallResolutionResult(resolvedCall, allDiagnostics, constraintSystem) } return if (type == ConstraintSystemCompletionMode.FULL) { - CompletedCallResolutionResult(resolvedCall, allDiagnostics, systemStorage) + CompletedCallResolutionResult(resolvedCall, allDiagnostics, constraintSystem) } else { - PartialCallResolutionResult(resolvedCall, allDiagnostics, systemStorage, forwardToInferenceSession) + PartialCallResolutionResult(resolvedCall, allDiagnostics, constraintSystem, forwardToInferenceSession) } } } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt index b52aba1ab92..5b405b6a0e1 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/DescriptorRelatedInferenceUtils.kt @@ -70,7 +70,7 @@ fun CallableDescriptor.substituteAndApproximateTypes( fun PostponedArgumentsAnalyzerContext.addSubsystemFromArgument(argument: KotlinCallArgument?): Boolean { return when (argument) { is SubKotlinCallArgument -> { - addOtherSystem(argument.callResult.constraintSystem) + addOtherSystem(argument.callResult.constraintSystem.getBuilder().currentStorage()) true } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt index 35cd989794d..5fe6c8185da 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/model/ResolutionAtoms.kt @@ -15,6 +15,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.* import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.calls.components.candidate.ResolutionCandidate import org.jetbrains.kotlin.resolve.calls.components.candidate.CallableReferenceResolutionCandidate +import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.TypeConstructor @@ -252,7 +253,7 @@ class ResolvedCollectionLiteralAtom( sealed class CallResolutionResult( resultCallAtom: ResolvedCallAtom?, val diagnostics: List, - val constraintSystem: ConstraintStorage + val constraintSystem: NewConstraintSystem ) : ResolvedAtom() { init { setAnalyzedResults(listOfNotNull(resultCallAtom)) @@ -283,31 +284,32 @@ sealed class CallResolutionResult( open class SingleCallResolutionResult( val resultCallAtom: ResolvedCallAtom, diagnostics: List, - constraintSystem: ConstraintStorage + constraintSystem: NewConstraintSystem ) : CallResolutionResult(resultCallAtom, diagnostics, constraintSystem) class PartialCallResolutionResult( resultCallAtom: ResolvedCallAtom, diagnostics: List, - constraintSystem: ConstraintStorage, + constraintSystem: NewConstraintSystem, val forwardToInferenceSession: Boolean = false ) : SingleCallResolutionResult(resultCallAtom, diagnostics, constraintSystem) class CompletedCallResolutionResult( resultCallAtom: ResolvedCallAtom, diagnostics: List, - constraintSystem: ConstraintStorage + constraintSystem: NewConstraintSystem ) : SingleCallResolutionResult(resultCallAtom, diagnostics, constraintSystem) class ErrorCallResolutionResult( resultCallAtom: ResolvedCallAtom, diagnostics: List, - constraintSystem: ConstraintStorage + constraintSystem: NewConstraintSystem ) : SingleCallResolutionResult(resultCallAtom, diagnostics, constraintSystem) class AllCandidatesResolutionResult( - val allCandidates: Collection -) : CallResolutionResult(null, emptyList(), ConstraintStorage.Empty) + val allCandidates: Collection, + constraintSystem: NewConstraintSystem +) : CallResolutionResult(null, emptyList(), constraintSystem) data class CandidateWithDiagnostics(val candidate: ResolutionCandidate, val diagnostics: List)