FIR: commonize builder & inference sessions
This commit is contained in:
committed by
teamcity
parent
e872cfa69a
commit
30ea1601d5
+49
@@ -11,9 +11,17 @@ import org.jetbrains.kotlin.fir.resolve.BodyResolveComponents
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.ResolutionContext
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.candidate
|
||||
import org.jetbrains.kotlin.fir.resolve.inference.model.ConeBuilderInferenceSubstitutionConstraintPosition
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.types.ConeKotlinType
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.components.ConstraintSystemCompletionContext
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintKind
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.InitialConstraint
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl
|
||||
import org.jetbrains.kotlin.types.model.StubTypeMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeVariableMarker
|
||||
import org.jetbrains.kotlin.types.model.safeSubstitute
|
||||
|
||||
abstract class AbstractManyCandidatesInferenceSession(
|
||||
protected val resolutionContext: ResolutionContext
|
||||
@@ -48,4 +56,45 @@ abstract class AbstractManyCandidatesInferenceSession(
|
||||
completedCalls.clear()
|
||||
}
|
||||
|
||||
protected fun integrateConstraintToSystem(
|
||||
commonSystem: NewConstraintSystemImpl,
|
||||
initialConstraint: InitialConstraint,
|
||||
callSubstitutor: ConeSubstitutor,
|
||||
nonFixedToVariablesSubstitutor: ConeSubstitutor,
|
||||
): Boolean {
|
||||
val substitutedConstraint = initialConstraint.substitute(callSubstitutor)
|
||||
val lower =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(substitutedConstraint.a as ConeKotlinType) // TODO: SUB
|
||||
val upper =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(substitutedConstraint.b as ConeKotlinType) // TODO: SUB
|
||||
|
||||
if (commonSystem.isProperType(lower) && (lower == upper || commonSystem.isProperType(upper))) return false
|
||||
|
||||
when (initialConstraint.constraintKind) {
|
||||
ConstraintKind.LOWER -> error("LOWER constraint shouldn't be used, please use UPPER")
|
||||
|
||||
ConstraintKind.UPPER -> commonSystem.addSubtypeConstraint(lower, upper, substitutedConstraint.position)
|
||||
|
||||
ConstraintKind.EQUALITY ->
|
||||
with(commonSystem) {
|
||||
addSubtypeConstraint(lower, upper, substitutedConstraint.position)
|
||||
addSubtypeConstraint(upper, lower, substitutedConstraint.position)
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
protected fun InitialConstraint.substitute(substitutor: TypeSubstitutorMarker): InitialConstraint {
|
||||
val lowerSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.a)
|
||||
val upperSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.b)
|
||||
|
||||
if (lowerSubstituted == a && upperSubstituted == b) return this
|
||||
|
||||
return InitialConstraint(
|
||||
lowerSubstituted,
|
||||
upperSubstituted,
|
||||
this.constraintKind,
|
||||
ConeBuilderInferenceSubstitutionConstraintPosition(this) // TODO
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-38
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.fir.expressions.FirResolvable
|
||||
import org.jetbrains.kotlin.fir.expressions.FirStatement
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.Candidate
|
||||
import org.jetbrains.kotlin.fir.resolve.calls.ResolutionContext
|
||||
import org.jetbrains.kotlin.fir.resolve.inference.model.ConeBuilderInferenceSubstitutionConstraintPosition
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor
|
||||
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||
import org.jetbrains.kotlin.fir.types.*
|
||||
@@ -27,9 +26,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.model.*
|
||||
import org.jetbrains.kotlin.resolve.calls.inference.registerTypeVariableIfNotPresent
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.BUILDER_INFERENCE_ANNOTATION_FQ_NAME
|
||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
|
||||
import org.jetbrains.kotlin.types.model.TypeVariableMarker
|
||||
import org.jetbrains.kotlin.types.model.safeSubstitute
|
||||
|
||||
class FirBuilderInferenceSession(
|
||||
private val lambda: FirAnonymousFunction,
|
||||
@@ -200,27 +197,8 @@ class FirBuilderInferenceSession(
|
||||
|
||||
for (initialConstraint in storage.initialConstraints) {
|
||||
if (initialConstraint.position is BuilderInferencePosition) continue
|
||||
val substitutedConstraint = initialConstraint.substitute(callSubstitutor)
|
||||
|
||||
val lower =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(substitutedConstraint.a as ConeKotlinType) // TODO: SUB
|
||||
val upper =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(substitutedConstraint.b as ConeKotlinType) // TODO: SUB
|
||||
|
||||
if (commonSystem.isProperType(lower) && commonSystem.isProperType(upper)) continue
|
||||
|
||||
introducedConstraint = true
|
||||
|
||||
when (initialConstraint.constraintKind) {
|
||||
ConstraintKind.LOWER -> error("LOWER constraint shouldn't be used, please use UPPER")
|
||||
|
||||
ConstraintKind.UPPER -> commonSystem.addSubtypeConstraint(lower, upper, substitutedConstraint.position)
|
||||
|
||||
ConstraintKind.EQUALITY ->
|
||||
with(commonSystem) {
|
||||
addSubtypeConstraint(lower, upper, substitutedConstraint.position)
|
||||
addSubtypeConstraint(upper, lower, substitutedConstraint.position)
|
||||
}
|
||||
if (integrateConstraintToSystem(commonSystem, initialConstraint, callSubstitutor, nonFixedToVariablesSubstitutor)) {
|
||||
introducedConstraint = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,20 +214,6 @@ class FirBuilderInferenceSession(
|
||||
return introducedConstraint
|
||||
}
|
||||
|
||||
private fun InitialConstraint.substitute(substitutor: TypeSubstitutorMarker): InitialConstraint {
|
||||
val lowerSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.a)
|
||||
val upperSubstituted = substitutor.safeSubstitute(resolutionContext.typeContext, this.b)
|
||||
|
||||
if (lowerSubstituted == a && upperSubstituted == b) return this
|
||||
|
||||
return InitialConstraint(
|
||||
lowerSubstituted,
|
||||
upperSubstituted,
|
||||
this.constraintKind,
|
||||
ConeBuilderInferenceSubstitutionConstraintPosition(this) // TODO
|
||||
)
|
||||
}
|
||||
|
||||
private fun updateCalls(commonSystem: NewConstraintSystemImpl) {
|
||||
val nonFixedToVariablesSubstitutor = createNonFixedTypeToVariableSubstitutor()
|
||||
val commonSystemSubstitutor = commonSystem.buildCurrentSubstitutor() as ConeSubstitutor
|
||||
|
||||
+2
-19
@@ -306,25 +306,8 @@ class FirDelegatedPropertyInferenceSession(
|
||||
var introducedConstraint = false
|
||||
|
||||
for (initialConstraint in storage.initialConstraints) {
|
||||
val lower =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(callSubstitutor.substituteOrSelf(initialConstraint.a as ConeKotlinType)) // TODO: SUB
|
||||
val upper =
|
||||
nonFixedToVariablesSubstitutor.substituteOrSelf(callSubstitutor.substituteOrSelf(initialConstraint.b as ConeKotlinType)) // TODO: SUB
|
||||
|
||||
if (commonSystem.isProperType(lower) && (lower == upper || commonSystem.isProperType(upper))) continue
|
||||
|
||||
introducedConstraint = true
|
||||
|
||||
when (initialConstraint.constraintKind) {
|
||||
ConstraintKind.LOWER -> error("LOWER constraint shouldn't be used, please use UPPER")
|
||||
|
||||
ConstraintKind.UPPER -> commonSystem.addSubtypeConstraint(lower, upper, initialConstraint.position)
|
||||
|
||||
ConstraintKind.EQUALITY ->
|
||||
with(commonSystem) {
|
||||
addSubtypeConstraint(lower, upper, initialConstraint.position)
|
||||
addSubtypeConstraint(upper, lower, initialConstraint.position)
|
||||
}
|
||||
if (integrateConstraintToSystem(commonSystem, initialConstraint, callSubstitutor, nonFixedToVariablesSubstitutor)) {
|
||||
introducedConstraint = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user