From aae1681b761aa75240cdc0fb0fd98ea7e826fceb Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 26 Aug 2020 12:45:13 +0300 Subject: [PATCH] [NI] Get rid of ResolvedAtom usage in ConstraintSystemCompletionContext --- .../calls/CreateFreshTypeVariableSubstitutorStage.kt | 4 ++-- .../fir/resolve/inference/ConstraintSystemCompleter.kt | 9 +++++++-- .../inference/model/FirConstraintPositionAndErrors.kt | 6 +++++- .../components/ConstraintSystemCompletionContext.kt | 4 ++-- .../components/KotlinConstraintSystemCompleter.kt | 7 ++++--- .../components/PostponedArgumentInputTypesResolver.kt | 5 +++-- .../calls/inference/model/NewConstraintSystemImpl.kt | 5 ++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt index 26617d0d2ea..52a94184c02 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/CreateFreshTypeVariableSubstitutorStage.kt @@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRef import org.jetbrains.kotlin.fir.declarations.FirTypeParameterRefsOwner import org.jetbrains.kotlin.fir.renderWithType import org.jetbrains.kotlin.fir.resolve.inference.TypeParameterBasedTypeVariable +import org.jetbrains.kotlin.fir.resolve.inference.model.ConeDeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap import org.jetbrains.kotlin.fir.symbols.StandardClassIds @@ -17,7 +18,6 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.FirTypePlaceholderProjection import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemOperation -import org.jetbrains.kotlin.fir.resolve.inference.model.FirDeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition internal object CreateFreshTypeVariableSubstitutorStage : ResolutionStage() { @@ -137,7 +137,7 @@ private fun createToFreshVariableSubstitutorAndAddInitialConstraints( csBuilder.addSubtypeConstraint( defaultType, toFreshVariables.substituteOrSelf(upperBound), - FirDeclaredUpperBoundConstraintPosition() + ConeDeclaredUpperBoundConstraintPosition() ) } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt index f4ca586c4a9..9481c1dbc5c 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/ConstraintSystemCompleter.kt @@ -9,11 +9,15 @@ import org.jetbrains.kotlin.fir.expressions.* 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.inference.model.ConeFixVariableConstraintPosition 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.* +import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionContext +import org.jetbrains.kotlin.resolve.calls.inference.components.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.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.VariableWithConstraints @@ -203,7 +207,8 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents) { ) { val direction = TypeVariableDirectionCalculator(c, postponedResolveKtPrimitives, topLevelType).getDirection(variableWithConstraints) val resultType = components.inferenceComponents.resultTypeResolver.findResultType(c, variableWithConstraints, direction) - c.fixVariable(variableWithConstraints.typeVariable, resultType, atom = null) // TODO: obtain atom for diagnostics + val variable = variableWithConstraints.typeVariable + c.fixVariable(variable, resultType, ConeFixVariableConstraintPosition(variable)) // TODO: obtain atom for diagnostics } private fun analyzePostponeArgumentIfPossible( diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt index fc54f4f4893..109d00e8af7 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/inference/model/FirConstraintPositionAndErrors.kt @@ -6,5 +6,9 @@ package org.jetbrains.kotlin.fir.resolve.inference.model import org.jetbrains.kotlin.resolve.calls.inference.model.DeclaredUpperBoundConstraintPosition +import org.jetbrains.kotlin.resolve.calls.inference.model.FixVariableConstraintPosition +import org.jetbrains.kotlin.types.model.TypeVariableMarker -class FirDeclaredUpperBoundConstraintPosition : DeclaredUpperBoundConstraintPosition(null) +class ConeDeclaredUpperBoundConstraintPosition : DeclaredUpperBoundConstraintPosition(null) + +class ConeFixVariableConstraintPosition(variable: TypeVariableMarker) : FixVariableConstraintPosition(variable, null) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt index a7e4c420e2e..e99355c484a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/ConstraintSystemCompletionContext.kt @@ -7,8 +7,8 @@ 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.FixVariableConstraintPosition 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 @@ -28,7 +28,7 @@ interface ConstraintSystemCompletionContext : VariableFixationFinder.Context, Re // mutable operations fun addError(error: ConstraintSystemError) - fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?) + fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, position: FixVariableConstraintPosition<*>) fun asConstraintSystemCompletionContext(): ConstraintSystemCompletionContext } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt index 29ae267e3dc..550788962a2 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/KotlinConstraintSystemCompleter.kt @@ -238,8 +238,9 @@ class KotlinConstraintSystemCompleter( topLevelAtoms: List ) { val resultType = resultTypeResolver.findResultType(c, variableWithConstraints, direction) - val resolvedAtom = findResolvedAtomBy(variableWithConstraints.typeVariable, topLevelAtoms) ?: topLevelAtoms.firstOrNull() - c.fixVariable(variableWithConstraints.typeVariable, resultType, resolvedAtom) + val variable = variableWithConstraints.typeVariable + val resolvedAtom = findResolvedAtomBy(variable, topLevelAtoms) ?: topLevelAtoms.firstOrNull() + c.fixVariable(variable, resultType, FixVariableConstraintPositionImpl(variable, resolvedAtom)) } private fun ConstraintSystemCompletionContext.fixVariablesOrReportNotEnoughInformation( @@ -300,7 +301,7 @@ class KotlinConstraintSystemCompleter( else -> ErrorUtils.createErrorType("Cannot infer type variable $typeVariable") } - c.fixVariable(typeVariable, resultErrorType, resolvedAtom) + c.fixVariable(typeVariable, resultErrorType, FixVariableConstraintPositionImpl(typeVariable, resolvedAtom)) } private fun ConstraintSystemCompletionContext.getOrderedAllTypeVariables( diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt index e433c0015e7..53e8b948ed2 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/PostponedArgumentInputTypesResolver.kt @@ -468,10 +468,11 @@ class PostponedArgumentInputTypesResolver( val variableWithConstraints = notFixedTypeVariables.getValue(variableForFixation.variable) val resultType = resultTypeResolver.findResultType(this, variableWithConstraints, TypeVariableDirectionCalculator.ResolveDirection.UNKNOWN) - val resolvedAtom = KotlinConstraintSystemCompleter.findResolvedAtomBy(variableWithConstraints.typeVariable, topLevelAtoms) + val variable = variableWithConstraints.typeVariable + val resolvedAtom = KotlinConstraintSystemCompleter.findResolvedAtomBy(variable, topLevelAtoms) ?: topLevelAtoms.firstOrNull() - fixVariable(variableWithConstraints.typeVariable, resultType, resolvedAtom) + fixVariable(variable, resultType, FixVariableConstraintPositionImpl(variable, resolvedAtom)) return true } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt index 6ed57b05b82..78a3213094a 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/NewConstraintSystemImpl.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.resolve.calls.inference.model import org.jetbrains.kotlin.resolve.calls.components.PostponedArgumentsAnalyzerContext import org.jetbrains.kotlin.resolve.calls.inference.* 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 import org.jetbrains.kotlin.types.model.* @@ -292,11 +291,11 @@ class NewConstraintSystemImpl( // KotlinConstraintSystemCompleter.Context // TODO: simplify this: do only substitution a fixing type variable rather than running of subtyping and full incorporation - override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, atom: ResolvedAtom?) { + override fun fixVariable(variable: TypeVariableMarker, resultType: KotlinTypeMarker, position: FixVariableConstraintPosition<*>) { checkState(State.BUILDING, State.COMPLETION) constraintInjector.addInitialEqualityConstraint( - this, variable.defaultType(), resultType, FixVariableConstraintPositionImpl(variable, atom) + this, variable.defaultType(), resultType, position ) val freshTypeConstructor = variable.freshTypeConstructor()