FIR: extract some inference-related type substitutors to classes
This commit is contained in:
committed by
teamcity
parent
97ed14cdee
commit
6614e1f3af
+26
-1
@@ -13,9 +13,10 @@ import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
|
|||||||
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
|
import org.jetbrains.kotlin.types.TypeApproximatorConfiguration
|
||||||
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||||
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
|
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
|
||||||
|
import org.jetbrains.kotlin.types.model.TypeVariableMarker
|
||||||
import org.jetbrains.kotlin.types.model.typeConstructor
|
import org.jetbrains.kotlin.types.model.typeConstructor
|
||||||
|
|
||||||
abstract class AbstractConeSubstitutor(private val typeContext: ConeTypeContext) : ConeSubstitutor() {
|
abstract class AbstractConeSubstitutor(protected val typeContext: ConeTypeContext) : ConeSubstitutor() {
|
||||||
private fun wrapProjection(old: ConeTypeProjection, newType: ConeKotlinType): ConeTypeProjection {
|
private fun wrapProjection(old: ConeTypeProjection, newType: ConeKotlinType): ConeTypeProjection {
|
||||||
return when (old) {
|
return when (old) {
|
||||||
is ConeStarProjection -> old
|
is ConeStarProjection -> old
|
||||||
@@ -204,3 +205,27 @@ fun createTypeSubstitutorByTypeConstructor(map: Map<TypeConstructorMarker, ConeK
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal class TypeSubstitutorByTypeConstructor(
|
||||||
|
private val map: Map<TypeConstructorMarker, ConeKotlinType>,
|
||||||
|
context: ConeTypeContext
|
||||||
|
) : AbstractConeSubstitutor(context), TypeSubstitutorMarker {
|
||||||
|
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
|
||||||
|
if (type !is ConeLookupTagBasedType && type !is ConeStubType) return null
|
||||||
|
val new = map[type.typeConstructor(typeContext)] ?: return null
|
||||||
|
return new.approximateIntegerLiteralType().updateNullabilityIfNeeded(type)?.withCombinedAttributesFrom(type, typeContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: builder inference uses TypeSubstitutorByTypeConstructor for not fixed type substitution
|
||||||
|
class NotFixedTypeToVariableSubstitutorForDelegateInference(
|
||||||
|
private val bindings: Map<TypeVariableMarker, ConeKotlinType>,
|
||||||
|
typeContext: ConeTypeContext
|
||||||
|
) : AbstractConeSubstitutor(typeContext) {
|
||||||
|
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
|
||||||
|
if (type !is ConeStubType) return null
|
||||||
|
if (type.constructor.isTypeVariableInSubtyping) return null
|
||||||
|
return bindings[type.constructor.variable].updateNullabilityIfNeeded(type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -165,14 +165,14 @@ class FirBuilderInferenceSession(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createNonFixedTypeToVariableSubstitutor(): ConeSubstitutor {
|
private fun createNonFixedTypeToVariableSubstitutor(): ConeSubstitutor {
|
||||||
val ctx = components.session.typeContext
|
val typeContext = components.session.typeContext
|
||||||
|
|
||||||
val bindings = mutableMapOf<TypeConstructorMarker, ConeKotlinType>()
|
val bindings = mutableMapOf<TypeConstructorMarker, ConeKotlinType>()
|
||||||
for ((variable, nonFixedType) in stubsForPostponedVariables) {
|
for ((variable, nonFixedType) in stubsForPostponedVariables) {
|
||||||
bindings[nonFixedType.constructor] = variable.defaultType
|
bindings[nonFixedType.constructor] = variable.defaultType
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.typeSubstitutorByTypeConstructor(bindings)
|
return typeContext.typeSubstitutorByTypeConstructor(bindings)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun integrateConstraints(
|
private fun integrateConstraints(
|
||||||
|
|||||||
+3
-9
@@ -10,9 +10,9 @@ import org.jetbrains.kotlin.fir.expressions.FirResolvable
|
|||||||
import org.jetbrains.kotlin.fir.expressions.FirStatement
|
import org.jetbrains.kotlin.fir.expressions.FirStatement
|
||||||
import org.jetbrains.kotlin.fir.resolve.calls.*
|
import org.jetbrains.kotlin.fir.resolve.calls.*
|
||||||
import org.jetbrains.kotlin.fir.resolve.inference.model.ConeFixVariableConstraintPosition
|
import org.jetbrains.kotlin.fir.resolve.inference.model.ConeFixVariableConstraintPosition
|
||||||
import org.jetbrains.kotlin.fir.resolve.substitution.AbstractConeSubstitutor
|
|
||||||
import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor
|
import org.jetbrains.kotlin.fir.resolve.substitution.ChainedSubstitutor
|
||||||
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
import org.jetbrains.kotlin.fir.resolve.substitution.ConeSubstitutor
|
||||||
|
import org.jetbrains.kotlin.fir.resolve.substitution.NotFixedTypeToVariableSubstitutorForDelegateInference
|
||||||
import org.jetbrains.kotlin.fir.types.*
|
import org.jetbrains.kotlin.fir.types.*
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
|
import org.jetbrains.kotlin.resolve.calls.inference.NewConstraintSystem
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor
|
import org.jetbrains.kotlin.resolve.calls.inference.buildAbstractResultingSubstitutor
|
||||||
@@ -110,7 +110,7 @@ class FirDelegatedPropertyInferenceSession(
|
|||||||
completionMode: ConstraintSystemCompletionMode
|
completionMode: ConstraintSystemCompletionMode
|
||||||
): Map<ConeTypeVariableTypeConstructor, ConeKotlinType>? = null
|
): Map<ConeTypeVariableTypeConstructor, ConeKotlinType>? = null
|
||||||
|
|
||||||
private fun createNonFixedTypeToVariableSubstitutor(): ConeSubstitutor {
|
private fun createNonFixedTypeToVariableSubstitutor(): NotFixedTypeToVariableSubstitutorForDelegateInference {
|
||||||
val typeContext = components.session.typeContext
|
val typeContext = components.session.typeContext
|
||||||
|
|
||||||
val bindings = mutableMapOf<TypeVariableMarker, ConeKotlinType>()
|
val bindings = mutableMapOf<TypeVariableMarker, ConeKotlinType>()
|
||||||
@@ -118,13 +118,7 @@ class FirDelegatedPropertyInferenceSession(
|
|||||||
bindings[synthetic] = variable.defaultType(typeContext) as ConeKotlinType
|
bindings[synthetic] = variable.defaultType(typeContext) as ConeKotlinType
|
||||||
}
|
}
|
||||||
|
|
||||||
return object : AbstractConeSubstitutor(typeContext) {
|
return NotFixedTypeToVariableSubstitutorForDelegateInference(bindings, typeContext)
|
||||||
override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
|
|
||||||
if (type !is ConeStubType) return null
|
|
||||||
if (type.constructor.isTypeVariableInSubtyping) return null
|
|
||||||
return bindings[type.constructor.variable].updateNullabilityIfNeeded(type)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user