[NI] Extract common parts from KotlinConstraintSystemCompleter

This commit is contained in:
Dmitriy Novozhilov
2020-08-26 11:28:08 +03:00
parent 12fbb93871
commit b15f847943
19 changed files with 107 additions and 91 deletions
@@ -9,8 +9,8 @@ import org.jetbrains.kotlin.fir.resolve.calls.Candidate
import org.jetbrains.kotlin.fir.types.ConeIntegerLiteralType
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeRef
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.Context
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionContext
import org.jetbrains.kotlin.resolve.calls.inference.components.TrivialConstraintTypeInferenceOracle
import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
@@ -40,7 +40,7 @@ fun Candidate.computeCompletionMode(
}
}
private typealias CsCompleterContext = Context
private typealias CsCompleterContext = ConstraintSystemCompletionContext
private class CalculatorForNestedCall(
private val candidate: Candidate,
@@ -13,10 +13,7 @@ import org.jetbrains.kotlin.fir.returnExpressions
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.TypeVariableDirectionCalculator
import org.jetbrains.kotlin.resolve.calls.inference.components.VariableFixationFinder
import org.jetbrains.kotlin.resolve.calls.inference.components.*
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
@@ -31,7 +28,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
val variableFixationFinder = VariableFixationFinder(components.inferenceComponents.trivialConstraintTypeInferenceOracle)
fun complete(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
completionMode: ConstraintSystemCompletionMode,
topLevelAtoms: List<FirStatement>,
candidateReturnType: ConeKotlinType,
@@ -77,7 +74,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
}
private fun resolveLambdaOrCallableReferenceWithTypeVariableAsExpectedType(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
variableForFixation: VariableFixationFinder.VariableForFixation,
postponedAtoms: List<PostponedResolvedAtom>,
/*diagnosticsHolder: KotlinDiagnosticsHolder,*/
@@ -122,7 +119,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
}
private inline fun <T : PostponedResolvedAtom, V : ConeTypeVariable> T.preparePostponedAtomWithTypeVariableAsExpectedType(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
csBuilder: ConstraintSystemBuilder,
variable: ConeTypeVariable,
parameterTypes: Array<out ConeKotlinType?>?,
@@ -161,7 +158,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
private fun getOrderedAllTypeVariables(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
topLevelAtoms: List<FirStatement>,
collectVariablesFromContext: Boolean
): List<TypeConstructorMarker> {
@@ -199,7 +196,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
}
private fun fixVariable(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
topLevelType: KotlinTypeMarker,
variableWithConstraints: VariableWithConstraints,
postponedResolveKtPrimitives: List<PostponedResolvedAtom>
@@ -210,7 +207,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
}
private fun analyzePostponeArgumentIfPossible(
c: KotlinConstraintSystemCompleter.Context,
c: ConstraintSystemCompletionContext,
topLevelAtoms: List<FirStatement>,
analyze: (PostponedResolvedAtom) -> Unit
): Boolean {
@@ -240,7 +237,7 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) {
return notAnalyzedArguments
}
private fun canWeAnalyzeIt(c: KotlinConstraintSystemCompleter.Context, argument: PostponedResolvedAtomMarker): Boolean {
private fun canWeAnalyzeIt(c: ConstraintSystemCompletionContext, argument: PostponedResolvedAtomMarker): Boolean {
if (argument.analyzed) return false
return argument.inputTypes.all { c.containsOnlyFixedOrPostponedVariables(it) }
}
@@ -16,7 +16,7 @@ import org.jetbrains.kotlin.fir.types.ConeTypeVariable
import org.jetbrains.kotlin.fir.types.ConeTypeVariableTypeConstructor
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.CoroutinePosition
@@ -94,7 +94,7 @@ class FirBuilderInferenceSession(
@Suppress("UNCHECKED_CAST")
components.callCompleter.completer.complete(
context,
KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL,
ConstraintSystemCompletionMode.FULL,
partiallyResolvedCalls.map { it.first as FirStatement },
components.session.builtinTypes.unitType.type,
collectVariablesFromContext = true
@@ -28,7 +28,7 @@ import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
import org.jetbrains.kotlin.types.model.StubTypeMarker
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.types.*
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.ConstraintSystemCompletionMode
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
@@ -69,7 +70,7 @@ class FirDelegatedPropertyInferenceSession(
@Suppress("UNCHECKED_CAST")
components.callCompleter.completer.complete(
commonSystem.asConstraintSystemCompleterContext(),
KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL,
ConstraintSystemCompletionMode.FULL,
resolvedCalls as List<FirStatement>,
unitType
) {
@@ -137,4 +138,4 @@ class FirDelegatedPropertyInferenceSession(
}
override fun <T> writeOnlyStubs(call: T): Boolean where T : FirResolvable, T : FirStatement = false
}
}
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.descriptors.VariableDescriptorWithAccessors
import org.jetbrains.kotlin.resolve.calls.components.*
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
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.DelegatedPropertyConstraintPositionImpl
@@ -82,7 +83,7 @@ class DelegatedPropertyInferenceSession(
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -104,7 +105,7 @@ class InferenceSessionForExistingCandidates(private val resolveReceiverIndepende
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -116,7 +117,7 @@ class InferenceSessionForExistingCandidates(private val resolveReceiverIndepende
override fun computeCompletionMode(
candidate: KotlinResolutionCandidate
): KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode? = null
): ConstraintSystemCompletionMode? = null
override fun resolveReceiverIndependently(): Boolean = resolveReceiverIndependently
}
@@ -20,6 +20,7 @@ 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.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutorByConstructorMap
@@ -169,7 +170,7 @@ class CoroutineInferenceSession(
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder,
): Map<TypeConstructor, UnwrappedType>? {
val (commonSystem, effectivelyEmptyConstraintSystem) = buildCommonSystem(initialStorage)
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.resolve.calls.components.*
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
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
@@ -81,7 +82,7 @@ abstract class ManyCandidatesResolver<D : CallableDescriptor>(
}
fun runCompletion(constraintSystem: NewConstraintSystem, atoms: List<ResolvedAtom>) {
val completionMode = KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL
val completionMode = ConstraintSystemCompletionMode.FULL
kotlinConstraintSystemCompleter.runCompletion(
constraintSystem.asConstraintSystemCompleterContext(),
completionMode,
@@ -5,8 +5,9 @@
package org.jetbrains.kotlin.resolve.calls.components
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionContext
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.TrivialConstraintTypeInferenceOracle
import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
@@ -18,7 +19,7 @@ import org.jetbrains.kotlin.types.model.*
import org.jetbrains.kotlin.utils.newLinkedHashMapWithExpectedSize
import java.util.*
typealias CsCompleterContext = KotlinConstraintSystemCompleter.Context
typealias CsCompleterContext = ConstraintSystemCompletionContext
class CompletionModeCalculator {
companion object {
@@ -238,4 +239,4 @@ class CompletionModeCalculator {
}
}
}
}
}
@@ -5,7 +5,7 @@
package org.jetbrains.kotlin.resolve.calls.components
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.TypeConstructor
@@ -22,7 +22,7 @@ interface InferenceSession {
override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType> = emptyMap()
@@ -31,7 +31,7 @@ interface InferenceSession {
override fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom) = true
override fun computeCompletionMode(
candidate: KotlinResolutionCandidate
): KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode? = null
): ConstraintSystemCompletionMode? = null
override fun resolveReceiverIndependently(): Boolean = false
}
@@ -45,14 +45,14 @@ interface InferenceSession {
fun inferPostponedVariables(
lambda: ResolvedLambdaAtom,
initialStorage: ConstraintStorage,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
): Map<TypeConstructor, UnwrappedType>?
fun writeOnlyStubs(callInfo: SingleCallResolutionResult): Boolean
fun callCompleted(resolvedAtom: ResolvedAtom): Boolean
fun shouldCompleteResolvedSubAtomsOf(resolvedCallAtom: ResolvedCallAtom): Boolean
fun computeCompletionMode(candidate: KotlinResolutionCandidate): KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode?
fun computeCompletionMode(candidate: KotlinResolutionCandidate): ConstraintSystemCompletionMode?
fun resolveReceiverIndependently(): Boolean
}
@@ -12,7 +12,7 @@ import org.jetbrains.kotlin.descriptors.synthetic.SyntheticMemberDescriptor
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
import org.jetbrains.kotlin.resolve.calls.inference.addEqualityConstraintIfCompatible
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.TrivialConstraintTypeInferenceOracle
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage.Empty.hasContradiction
import org.jetbrains.kotlin.resolve.calls.inference.model.ExpectedTypeConstraintPositionImpl
@@ -13,7 +13,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.FilteredAnnotations
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.addSubsystemFromArgument
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.CoroutinePosition
import org.jetbrains.kotlin.resolve.calls.inference.model.LambdaArgumentConstraintPositionImpl
@@ -47,7 +47,7 @@ class PostponedArgumentsAnalyzer(
c: Context,
resolutionCallbacks: KotlinResolutionCallbacks,
argument: ResolvedAtom,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder
) {
when (argument) {
@@ -89,7 +89,7 @@ class PostponedArgumentsAnalyzer(
c: Context,
resolutionCallbacks: KotlinResolutionCallbacks,
lambda: ResolvedLambdaAtom,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticHolder: KotlinDiagnosticsHolder,
): ReturnArgumentsAnalysisResult {
val substitutorAndStubsForLambdaAnalysis = c.createSubstituteFunctorForLambdaAnalysis()
@@ -156,7 +156,7 @@ class PostponedArgumentsAnalyzer(
c: Context,
lambda: ResolvedLambdaAtom,
returnArgumentsAnalysisResult: ReturnArgumentsAnalysisResult,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
diagnosticHolder: KotlinDiagnosticsHolder,
substitute: (KotlinType) -> UnwrappedType = c.createSubstituteFunctorForLambdaAnalysis().substitute
) {
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.resolve.calls.inference
import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionContext
import org.jetbrains.kotlin.resolve.calls.inference.components.PostponedArgumentInputTypesResolver
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintSystemError
@@ -31,7 +31,7 @@ interface NewConstraintSystem {
// after this method we shouldn't mutate system via ConstraintSystemBuilder
fun asReadOnlyStorage(): ConstraintStorage
fun asConstraintSystemCompleterContext(): KotlinConstraintSystemCompleter.Context
fun asConstraintSystemCompleterContext(): ConstraintSystemCompletionContext
fun asPostponedArgumentsAnalyzerContext(): PostponedArgumentsAnalyzer.Context
fun asPostponedArgumentInputTypesResolverContext(): PostponedArgumentInputTypesResolver.Context
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintSystemError
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
import org.jetbrains.kotlin.resolve.calls.model.ResolvedAtom
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.types.model.TypeVariableMarker
interface ConstraintSystemCompletionContext : VariableFixationFinder.Context, ResultTypeResolver.Context {
val allTypeVariables: Map<TypeConstructorMarker, TypeVariableMarker>
override val notFixedTypeVariables: Map<TypeConstructorMarker, VariableWithConstraints>
override val postponedTypeVariables: List<TypeVariableMarker>
fun getBuilder(): ConstraintSystemBuilder
// type can be proper if it not contains not fixed type variables
fun canBeProper(type: KotlinTypeMarker): Boolean
fun containsOnlyFixedOrPostponedVariables(type: KotlinTypeMarker): Boolean
// mutable operations
fun addError(error: ConstraintSystemError)
fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?)
fun asPostponedArgumentInputTypesResolverContext(): PostponedArgumentInputTypesResolver.Context
}
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.calls.inference.components
enum class ConstraintSystemCompletionMode {
FULL,
PARTIAL,
UNTIL_FIRST_LAMBDA
}
@@ -6,11 +6,9 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.resolve.calls.components.transformToResolvedLambda
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder
import org.jetbrains.kotlin.resolve.calls.inference.model.*
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.types.model.TypeVariableMarker
import org.jetbrains.kotlin.types.model.safeSubstitute
@@ -24,34 +22,8 @@ class KotlinConstraintSystemCompleter(
) {
private val postponedArgumentInputTypesResolver = PostponedArgumentInputTypesResolver(resultTypeResolver, variableFixationFinder)
enum class ConstraintSystemCompletionMode {
FULL,
PARTIAL,
UNTIL_FIRST_LAMBDA
}
interface Context : VariableFixationFinder.Context, ResultTypeResolver.Context {
val allTypeVariables: Map<TypeConstructorMarker, TypeVariableMarker>
override val notFixedTypeVariables: Map<TypeConstructorMarker, VariableWithConstraints>
override val postponedTypeVariables: List<TypeVariableMarker>
fun getBuilder(): ConstraintSystemBuilder
// type can be proper if it not contains not fixed type variables
fun canBeProper(type: KotlinTypeMarker): Boolean
fun containsOnlyFixedOrPostponedVariables(type: KotlinTypeMarker): Boolean
// mutable operations
fun addError(error: ConstraintSystemError)
fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?)
fun asPostponedArgumentInputTypesResolverContext(): PostponedArgumentInputTypesResolver.Context
}
fun runCompletion(
c: Context,
c: ConstraintSystemCompletionContext,
completionMode: ConstraintSystemCompletionMode,
topLevelAtoms: List<ResolvedAtom>,
topLevelType: UnwrappedType,
@@ -69,7 +41,7 @@ class KotlinConstraintSystemCompleter(
}
fun completeConstraintSystem(
c: Context,
c: ConstraintSystemCompletionContext,
topLevelType: UnwrappedType,
topLevelAtoms: List<ResolvedAtom>,
completionMode: ConstraintSystemCompletionMode,
@@ -86,7 +58,7 @@ class KotlinConstraintSystemCompleter(
}
}
private fun Context.runCompletion(
private fun ConstraintSystemCompletionContext.runCompletion(
completionMode: ConstraintSystemCompletionMode,
topLevelAtoms: List<ResolvedAtom>,
topLevelType: UnwrappedType,
@@ -171,7 +143,7 @@ class KotlinConstraintSystemCompleter(
}
}
private fun Context.analyzeArgumentWithFixedParameterTypes(
private fun ConstraintSystemCompletionContext.analyzeArgumentWithFixedParameterTypes(
postponedArguments: List<PostponedResolvedAtom>,
analyze: (PostponedResolvedAtom) -> Unit
): Boolean {
@@ -185,7 +157,7 @@ class KotlinConstraintSystemCompleter(
return false
}
private fun Context.analyzeNextReadyPostponedArgument(
private fun ConstraintSystemCompletionContext.analyzeNextReadyPostponedArgument(
postponedArguments: List<PostponedResolvedAtom>,
completionMode: ConstraintSystemCompletionMode,
analyze: (PostponedResolvedAtom) -> Unit
@@ -239,7 +211,7 @@ class KotlinConstraintSystemCompleter(
return atom.transformToResolvedLambda(csBuilder, diagnosticsHolder, expectedType, returnVariable)
}
private fun Context.hasLambdaToAnalyze(
private fun ConstraintSystemCompletionContext.hasLambdaToAnalyze(
postponedArguments: List<PostponedResolvedAtom>
): Boolean {
return analyzeArgumentWithFixedParameterTypes(postponedArguments) {}
@@ -248,11 +220,11 @@ class KotlinConstraintSystemCompleter(
private fun findPostponedArgumentWithRevisableExpectedType(postponedArguments: List<PostponedResolvedAtom>) =
postponedArguments.firstOrNull { argument -> argument is PostponedAtomWithRevisableExpectedType }
private fun Context.findPostponedArgumentWithFixedOrPostponedInputTypes(postponedArguments: List<PostponedResolvedAtom>) =
private fun ConstraintSystemCompletionContext.findPostponedArgumentWithFixedOrPostponedInputTypes(postponedArguments: List<PostponedResolvedAtom>) =
postponedArguments.firstOrNull { argument -> argument.inputTypes.all { containsOnlyFixedOrPostponedVariables(it) } }
private fun fixVariable(
c: Context,
c: ConstraintSystemCompletionContext,
variableWithConstraints: VariableWithConstraints,
topLevelAtoms: List<ResolvedAtom>
) {
@@ -260,7 +232,7 @@ class KotlinConstraintSystemCompleter(
}
private fun fixVariable(
c: Context,
c: ConstraintSystemCompletionContext,
variableWithConstraints: VariableWithConstraints,
direction: TypeVariableDirectionCalculator.ResolveDirection,
topLevelAtoms: List<ResolvedAtom>
@@ -270,7 +242,7 @@ class KotlinConstraintSystemCompleter(
c.fixVariable(variableWithConstraints.typeVariable, resultType, resolvedAtom)
}
private fun Context.fixVariablesOrReportNotEnoughInformation(
private fun ConstraintSystemCompletionContext.fixVariablesOrReportNotEnoughInformation(
completionMode: ConstraintSystemCompletionMode,
topLevelAtoms: List<ResolvedAtom>,
topLevelType: UnwrappedType,
@@ -304,7 +276,7 @@ class KotlinConstraintSystemCompleter(
}
private fun processVariableWhenNotEnoughInformation(
c: Context,
c: ConstraintSystemCompletionContext,
variableWithConstraints: VariableWithConstraints,
topLevelAtoms: List<ResolvedAtom>,
diagnosticsHolder: KotlinDiagnosticsHolder
@@ -331,7 +303,7 @@ class KotlinConstraintSystemCompleter(
c.fixVariable(typeVariable, resultErrorType, resolvedAtom)
}
private fun Context.getOrderedAllTypeVariables(
private fun ConstraintSystemCompletionContext.getOrderedAllTypeVariables(
collectVariablesFromContext: Boolean,
topLevelAtoms: List<ResolvedAtom>
): List<TypeConstructorMarker> {
@@ -383,7 +355,7 @@ class KotlinConstraintSystemCompleter(
return result.toList()
}
private fun Context.isThereAnyReadyForFixationVariable(
private fun ConstraintSystemCompletionContext.isThereAnyReadyForFixationVariable(
completionMode: ConstraintSystemCompletionMode,
topLevelAtoms: List<ResolvedAtom>,
topLevelType: UnwrappedType,
@@ -20,7 +20,7 @@ class PostponedArgumentInputTypesResolver(
private val resultTypeResolver: ResultTypeResolver,
private val variableFixationFinder: VariableFixationFinder
) {
interface Context : KotlinConstraintSystemCompleter.Context
interface Context : ConstraintSystemCompletionContext
private class ParameterTypesInfo(
val parametersFromDeclaration: List<UnwrappedType?>?,
@@ -339,7 +339,7 @@ class PostponedArgumentInputTypesResolver(
fun collectParameterTypesAndBuildNewExpectedTypes(
c: Context,
postponedArguments: List<PostponedAtomWithRevisableExpectedType>,
completionMode: KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode,
completionMode: ConstraintSystemCompletionMode,
dependencyProvider: TypeVariableDependencyInformationProvider
): Boolean {
// We can collect parameter types from declaration in any mode, they can't change during completion.
@@ -357,7 +357,7 @@ class PostponedArgumentInputTypesResolver(
*
* TODO: investigate why we can't do it for anonymous functions in full mode always (see `diagnostics/tests/resolve/resolveWithSpecifiedFunctionLiteralWithId.kt`)
*/
if (completionMode == KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.PARTIAL && !isAnonymousFunction(argument))
if (completionMode == ConstraintSystemCompletionMode.PARTIAL && !isAnonymousFunction(argument))
return@any false
if (argument.revisedExpectedType != null) return@any false
val parameterTypesInfo =
@@ -459,7 +459,7 @@ class PostponedArgumentInputTypesResolver(
val relatedVariables = type.getPureArgumentsForFunctionalTypeOrSubtype()
.flatMap { getAllDeeplyRelatedTypeVariables(it, dependencyProvider) }
val variableForFixation = variableFixationFinder.findFirstVariableForFixation(
this, relatedVariables, postponedArguments, KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.FULL, topLevelType
this, relatedVariables, postponedArguments, ConstraintSystemCompletionMode.FULL, topLevelType
)
if (variableForFixation == null || !variableForFixation.hasProperConstraint)
@@ -16,8 +16,7 @@
package org.jetbrains.kotlin.resolve.calls.inference.components
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter.ConstraintSystemCompletionMode.PARTIAL
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionMode.PARTIAL
import org.jetbrains.kotlin.resolve.calls.inference.model.Constraint
import org.jetbrains.kotlin.resolve.calls.inference.model.DeclaredUpperBoundConstraintPosition
import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints
@@ -7,10 +7,7 @@ package org.jetbrains.kotlin.resolve.calls.inference.model
import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzer
import org.jetbrains.kotlin.resolve.calls.inference.*
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintInjector
import org.jetbrains.kotlin.resolve.calls.inference.components.KotlinConstraintSystemCompleter
import org.jetbrains.kotlin.resolve.calls.inference.components.PostponedArgumentInputTypesResolver
import org.jetbrains.kotlin.resolve.calls.inference.components.ResultTypeResolver
import org.jetbrains.kotlin.resolve.calls.inference.components.*
import org.jetbrains.kotlin.resolve.calls.model.ResolvedAtom
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.NewKotlinTypeChecker
@@ -30,7 +27,7 @@ class NewConstraintSystemImpl(
ConstraintSystemBuilder,
ConstraintInjector.Context,
ResultTypeResolver.Context,
KotlinConstraintSystemCompleter.Context,
ConstraintSystemCompletionContext,
PostponedArgumentInputTypesResolver.Context,
PostponedArgumentsAnalyzer.Context {
private val storage = MutableConstraintStorage()