FIR: Fix synthetic type variables to independent stub types

This commit is contained in:
Simon Ogorodnik
2021-11-26 16:51:42 +03:00
committed by teamcity
parent 580410863f
commit df9da371cb
4 changed files with 34 additions and 15 deletions
@@ -322,7 +322,11 @@ fun ConeIntersectionType.mapTypes(func: (ConeKotlinType) -> ConeKotlinType): Con
return ConeIntersectionType(intersectedTypes.map(func), alternativeType?.let(func)) return ConeIntersectionType(intersectedTypes.map(func), alternativeType?.let(func))
} }
data class ConeStubTypeConstructor(val variable: ConeTypeVariable, val isTypeVariableInSubtyping: Boolean) : TypeConstructorMarker data class ConeStubTypeConstructor(
val variable: ConeTypeVariable,
val isTypeVariableInSubtyping: Boolean,
val isForFixation: Boolean = false,
) : TypeConstructorMarker
sealed class ConeStubType(val constructor: ConeStubTypeConstructor, override val nullability: ConeNullability) : StubTypeMarker, sealed class ConeStubType(val constructor: ConeStubTypeConstructor, override val nullability: ConeNullability) : StubTypeMarker,
ConeSimpleKotlinType() { ConeSimpleKotlinType() {
@@ -363,6 +367,9 @@ open class ConeStubTypeForBuilderInference(constructor: ConeStubTypeConstructor,
) )
} }
class ConeStubTypeForFixation(constructor: ConeStubTypeConstructor, nullability: ConeNullability) :
ConeStubTypeForBuilderInference(constructor, nullability)
class ConeStubTypeForTypeVariableInSubtyping(constructor: ConeStubTypeConstructor, nullability: ConeNullability) : class ConeStubTypeForTypeVariableInSubtyping(constructor: ConeStubTypeConstructor, nullability: ConeNullability) :
ConeStubType(constructor, nullability) { ConeStubType(constructor, nullability) {
constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this( constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this(
@@ -435,6 +435,7 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
override fun TypeConstructorMarker.unwrapStubTypeVariableConstructor(): TypeConstructorMarker { override fun TypeConstructorMarker.unwrapStubTypeVariableConstructor(): TypeConstructorMarker {
if (this !is ConeStubTypeConstructor) return this if (this !is ConeStubTypeConstructor) return this
if (this.isTypeVariableInSubtyping) return this if (this.isTypeVariableInSubtyping) return this
if (this.isForFixation) return this
return this.variable.typeConstructor return this.variable.typeConstructor
} }
@@ -226,17 +226,23 @@ class ConstraintSystemCompleter(private val components: BodyResolveComponents, p
val variableWithConstraints = notFixedTypeVariables.getValue(variableForFixation.variable) val variableWithConstraints = notFixedTypeVariables.getValue(variableForFixation.variable)
if (variableForFixation.hasProperConstraint) { when {
variableForFixation.hasProperConstraint -> {
fixVariable(asConstraintSystemCompletionContext(), topLevelType, variableWithConstraints, postponedArguments) fixVariable(asConstraintSystemCompletionContext(), topLevelType, variableWithConstraints, postponedArguments)
return true return true
} else if (context.inferenceSession.isSyntheticTypeVariable(variableWithConstraints.typeVariable)) { }
val variable = variableWithConstraints.typeVariable as ConeTypeVariable context.inferenceSession.isSyntheticTypeVariable(variableWithConstraints.typeVariable) -> {
context.inferenceSession.fixSyntheticTypeVariableWithNotEnoughInformation(variable, asConstraintSystemCompletionContext()) context.inferenceSession.fixSyntheticTypeVariableWithNotEnoughInformation(
variableWithConstraints.typeVariable as ConeTypeVariable,
asConstraintSystemCompletionContext()
)
return true return true
} else { }
else -> {
processVariableWhenNotEnoughInformation(this, variableWithConstraints, topLevelAtoms) processVariableWhenNotEnoughInformation(this, variableWithConstraints, topLevelAtoms)
} }
} }
}
return false return false
} }
@@ -112,15 +112,16 @@ class FirDelegatedPropertyInferenceSession(
private fun createNonFixedTypeToVariableSubstitutor(): ConeSubstitutor { private fun createNonFixedTypeToVariableSubstitutor(): ConeSubstitutor {
val typeContext = components.session.typeContext val typeContext = components.session.typeContext
val bindings = mutableMapOf<TypeConstructorMarker, ConeKotlinType>() val bindings = mutableMapOf<TypeVariableMarker, ConeKotlinType>()
for ((variable, stubType) in stubTypesByTypeVariable) { for ((variable, synthetic) in syntheticTypeVariableByTypeVariable) {
bindings[stubType.constructor] = variable.defaultType(typeContext) as ConeKotlinType bindings[synthetic] = variable.defaultType(typeContext) as ConeKotlinType
} }
return object : AbstractConeSubstitutor(typeContext) { return object : AbstractConeSubstitutor(typeContext) {
override fun substituteType(type: ConeKotlinType): ConeKotlinType? { override fun substituteType(type: ConeKotlinType): ConeKotlinType? {
if (type !is ConeStubType) return null if (type !is ConeStubType) return null
return bindings[type.constructor].updateNullabilityIfNeeded(type) if (type.constructor.isTypeVariableInSubtyping) return null
return bindings[type.constructor.variable].updateNullabilityIfNeeded(type)
} }
} }
} }
@@ -151,9 +152,13 @@ class FirDelegatedPropertyInferenceSession(
typeVariable: TypeVariableMarker, typeVariable: TypeVariableMarker,
completionContext: ConstraintSystemCompletionContext completionContext: ConstraintSystemCompletionContext
) { ) {
typeVariable as ConeTypeVariable
completionContext.fixVariable( completionContext.fixVariable(
typeVariable, typeVariable,
stubTypeBySyntheticTypeVariable[typeVariable]!!, ConeStubTypeForFixation(
ConeStubTypeConstructor(typeVariable, isTypeVariableInSubtyping = false, isForFixation = true),
ConeNullability.create(typeVariable.defaultType.isMarkedNullable)
),
ConeFixVariableConstraintPosition(typeVariable) ConeFixVariableConstraintPosition(typeVariable)
) )
} }