Inject stub type variables of a different builder inference call properly

This commit is contained in:
Victor Petukhov
2021-05-21 15:00:26 +03:00
parent d486f7e188
commit 9fd1cbd2e7
4 changed files with 38 additions and 14 deletions
@@ -54,7 +54,8 @@ class BuilderInferenceSession(
private val deprecationResolver: DeprecationResolver, private val deprecationResolver: DeprecationResolver,
private val moduleDescriptor: ModuleDescriptor, private val moduleDescriptor: ModuleDescriptor,
private val typeApproximator: TypeApproximator, private val typeApproximator: TypeApproximator,
private val missingSupertypesResolver: MissingSupertypesResolver private val missingSupertypesResolver: MissingSupertypesResolver,
private val lambdaArgument: LambdaKotlinCallArgument
) : ManyCandidatesResolver<CallableDescriptor>( ) : ManyCandidatesResolver<CallableDescriptor>(
psiCallResolver, postponedArgumentsAnalyzer, kotlinConstraintSystemCompleter, callComponents, builtIns psiCallResolver, postponedArgumentsAnalyzer, kotlinConstraintSystemCompleter, callComponents, builtIns
) { ) {
@@ -204,9 +205,10 @@ class BuilderInferenceSession(
return lhs.isAncestor(callElement) return lhs.isAncestor(callElement)
} }
override fun currentConstraintSystem(): ConstraintStorage { override fun currentConstraintSystem() = ConstraintStorage.Empty
return ConstraintStorage.Empty
} fun getNotFixedToInferredTypesSubstitutor(): NewTypeSubstitutor =
ComposedSubstitutor(commonSystem.buildCurrentSubstitutor() as NewTypeSubstitutor, createNonFixedTypeToVariableSubstitutor())
override fun inferPostponedVariables( override fun inferPostponedVariables(
lambda: ResolvedLambdaAtom, lambda: ResolvedLambdaAtom,
@@ -214,14 +216,18 @@ class BuilderInferenceSession(
completionMode: ConstraintSystemCompletionMode, completionMode: ConstraintSystemCompletionMode,
diagnosticsHolder: KotlinDiagnosticsHolder, diagnosticsHolder: KotlinDiagnosticsHolder,
): Map<TypeConstructor, UnwrappedType>? { ): Map<TypeConstructor, UnwrappedType>? {
val effectivelyEmptyConstraintSystem = initializeCommonSystem(initialStorage)
val initialStorageSubstitutor = initialStorage.buildResultingSubstitutor(commonSystem, transformTypeVariablesToErrorTypes = false)
this.lambda = lambda this.lambda = lambda
fun getResultingSubstitutor(): NewTypeSubstitutor {
val storageSubstitutor = initialStorage.buildResultingSubstitutor(commonSystem, transformTypeVariablesToErrorTypes = false)
return ComposedSubstitutor(storageSubstitutor, commonSystem.buildCurrentSubstitutor() as NewTypeSubstitutor)
}
val effectivelyEmptyConstraintSystem = initializeCommonSystem(initialStorage)
if (effectivelyEmptyConstraintSystem) { if (effectivelyEmptyConstraintSystem) {
if (isTopLevelBuilderInferenceCall()) { if (isTopLevelBuilderInferenceCall()) {
updateAllCalls(initialStorageSubstitutor) updateAllCalls(getResultingSubstitutor())
} }
return null return null
} }
@@ -235,7 +241,7 @@ class BuilderInferenceSession(
) )
if (isTopLevelBuilderInferenceCall()) { if (isTopLevelBuilderInferenceCall()) {
updateAllCalls(initialStorageSubstitutor) updateAllCalls(getResultingSubstitutor())
} }
return commonSystem.fixedTypeVariables.cast() // TODO: SUB return commonSystem.fixedTypeVariables.cast() // TODO: SUB
@@ -250,12 +256,15 @@ class BuilderInferenceSession(
private fun updateAllCalls(substitutor: NewTypeSubstitutor) { private fun updateAllCalls(substitutor: NewTypeSubstitutor) {
updateCalls( updateCalls(
lambda, lambda,
substitutor = ComposedSubstitutor(substitutor, commonSystem.buildCurrentSubstitutor() as NewTypeSubstitutor), substitutor = substitutor,
commonSystem.errors commonSystem.errors
) )
for (nestedSession in nestedBuilderInferenceSessions) { for (nestedSession in nestedBuilderInferenceSessions) {
nestedSession.updateAllCalls(substitutor) // TODO: exclude injected variables
nestedSession.updateAllCalls(
ComposedSubstitutor(nestedSession.commonSystem.buildCurrentSubstitutor() as NewTypeSubstitutor, substitutor)
)
} }
} }
@@ -291,7 +300,14 @@ class BuilderInferenceSession(
} }
for (parentSession in findAllParentBuildInferenceSessions()) { for (parentSession in findAllParentBuildInferenceSessions()) {
parentSession.stubsForPostponedVariables.keys.forEach { commonSystem.registerVariable(it) } for ((variable, stubType) in parentSession.stubsForPostponedVariables) {
commonSystem.registerVariable(variable)
commonSystem.addSubtypeConstraint(
variable.defaultType,
stubType,
InjectedAnotherStubTypeConstraintPositionImpl(lambdaArgument)
)
}
} }
/* /*
@@ -435,7 +451,7 @@ class BuilderInferenceSession(
return currentSession.topLevelCallContext.trace return currentSession.topLevelCallContext.trace
} }
private fun completeDoubleColonExpression(expression: KtDoubleColonExpression, substitutor: NewTypeSubstitutor) { fun completeDoubleColonExpression(expression: KtDoubleColonExpression, substitutor: NewTypeSubstitutor) {
val atomCompleter = createResolvedAtomCompleter(substitutor, topLevelCallContext) val atomCompleter = createResolvedAtomCompleter(substitutor, topLevelCallContext)
val declarationDescriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, expression) val declarationDescriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, expression)
@@ -167,7 +167,7 @@ class KotlinResolutionCallbacksImpl(
callComponents, builtIns, topLevelCallContext, stubsForPostponedVariables, trace, callComponents, builtIns, topLevelCallContext, stubsForPostponedVariables, trace,
kotlinToResolvedCallTransformer, expressionTypingServices, argumentTypeResolver, kotlinToResolvedCallTransformer, expressionTypingServices, argumentTypeResolver,
doubleColonExpressionResolver, deprecationResolver, moduleDescriptor, typeApproximator, doubleColonExpressionResolver, deprecationResolver, moduleDescriptor, typeApproximator,
missingSupertypesResolver missingSupertypesResolver, lambdaArgument
) )
} else { } else {
null null
@@ -18,6 +18,11 @@ abstract class ExplicitTypeParameterConstraintPosition<T>(val typeArgument: T) :
override fun toString(): String = "TypeParameter $typeArgument" override fun toString(): String = "TypeParameter $typeArgument"
} }
abstract class InjectedAnotherStubTypeConstraintPosition<T>(private val builderInferenceLambdaOfInjectedStubType: T) : ConstraintPosition(),
OnlyInputTypeConstraintPosition {
override fun toString(): String = "Injected from $builderInferenceLambdaOfInjectedStubType builder inference call"
}
abstract class ExpectedTypeConstraintPosition<T>(val topLevelCall: T) : ConstraintPosition(), OnlyInputTypeConstraintPosition { abstract class ExpectedTypeConstraintPosition<T>(val topLevelCall: T) : ConstraintPosition(), OnlyInputTypeConstraintPosition {
override fun toString(): String = "ExpectedType for call $topLevelCall" override fun toString(): String = "ExpectedType for call $topLevelCall"
} }
@@ -15,6 +15,9 @@ class ExplicitTypeParameterConstraintPositionImpl(
typeArgument: SimpleTypeArgument typeArgument: SimpleTypeArgument
) : ExplicitTypeParameterConstraintPosition<SimpleTypeArgument>(typeArgument) ) : ExplicitTypeParameterConstraintPosition<SimpleTypeArgument>(typeArgument)
class InjectedAnotherStubTypeConstraintPositionImpl(builderInferenceLambdaOfInjectedStubType: LambdaKotlinCallArgument) :
InjectedAnotherStubTypeConstraintPosition<LambdaKotlinCallArgument>(builderInferenceLambdaOfInjectedStubType)
class ExpectedTypeConstraintPositionImpl(topLevelCall: KotlinCall) : ExpectedTypeConstraintPosition<KotlinCall>(topLevelCall) class ExpectedTypeConstraintPositionImpl(topLevelCall: KotlinCall) : ExpectedTypeConstraintPosition<KotlinCall>(topLevelCall)
class DeclaredUpperBoundConstraintPositionImpl( class DeclaredUpperBoundConstraintPositionImpl(