From 2ea7fb88015dfdb36e43892619861d2927b5717e Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Mon, 8 Apr 2019 15:47:35 +0300 Subject: [PATCH] Cleanup ConstraintStorage.buildResultingSubstitutor --- .../inference/CoroutineInferenceSession.kt | 2 +- .../tower/KotlinToResolvedCallTransformer.kt | 6 ++-- .../resolve/calls/tower/PSICallResolver.kt | 5 ++- .../resolve/calls/inference/InferenceUtils.kt | 33 ++++++++++--------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt index a37ca4a0397..2e992bec087 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/inference/CoroutineInferenceSession.kt @@ -116,7 +116,7 @@ class CoroutineInferenceSession( * * while substitutor from parameter map non-fixed types to the original type variable * */ - val callSubstitutor = storage.buildResultingSubstitutor(commonSystem) as NewTypeSubstitutor // substitutor only for fixed variables + val callSubstitutor = storage.buildResultingSubstitutor(commonSystem) // substitutor only for fixed variables for (initialConstraint in storage.initialConstraints) { val lower = nonFixedToVariablesSubstitutor.safeSubstitute(callSubstitutor.safeSubstitute(initialConstraint.a as UnwrappedType)) // TODO: SUB diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt index 2eab59ff988..96fdfbfdb16 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/KotlinToResolvedCallTransformer.kt @@ -49,6 +49,7 @@ import org.jetbrains.kotlin.types.expressions.DataFlowAnalyzer import org.jetbrains.kotlin.types.expressions.DoubleColonExpressionResolver import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils +import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContextDelegate import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -66,7 +67,8 @@ class KotlinToResolvedCallTransformer( private val additionalDiagnosticReporter: AdditionalDiagnosticReporter, private val moduleDescriptor: ModuleDescriptor, private val dataFlowValueFactory: DataFlowValueFactory, - private val builtIns: KotlinBuiltIns + private val builtIns: KotlinBuiltIns, + private val typeSystemContext: TypeSystemInferenceExtensionContextDelegate ) { companion object { private val REPORT_MISSING_NEW_INFERENCE_DIAGNOSTIC @@ -106,7 +108,7 @@ class KotlinToResolvedCallTransformer( is CompletedCallResolutionResult, is ErrorCallResolutionResult -> { val candidate = (baseResolvedCall as SingleCallResolutionResult).resultCallAtom - val resultSubstitutor = baseResolvedCall.constraintSystem.buildResultingSubstitutor() + val resultSubstitutor = baseResolvedCall.constraintSystem.buildResultingSubstitutor(typeSystemContext) if (context.inferenceSession.writeOnlyStubs()) { val stub = createStubResolvedCallAndWriteItToTrace( candidate, diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt index d15f7c8650c..b58f39a1e3b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt @@ -43,6 +43,7 @@ import org.jetbrains.kotlin.resolve.scopes.* import org.jetbrains.kotlin.resolve.scopes.receivers.* import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.expressions.* +import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult import java.util.* @@ -190,7 +191,9 @@ class PSICallResolver( ): OverloadResolutionResults { if (result is AllCandidatesResolutionResult) { val resolvedCalls = result.allCandidates.map { (candidate, diagnostics) -> - val resultingSubstitutor = candidate.getSystem().asReadOnlyStorage().buildResultingSubstitutor() + val system = candidate.getSystem() + val resultingSubstitutor = + system.asReadOnlyStorage().buildResultingSubstitutor(system as TypeSystemInferenceExtensionContext) kotlinToResolvedCallTransformer.transformToResolvedCall( candidate.resolvedCall, null, resultingSubstitutor, diagnostics diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt index e1f91eeb242..4d7d642db1b 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/InferenceUtils.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstituto import org.jetbrains.kotlin.resolve.calls.inference.model.ConstraintStorage import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.types.* +import org.jetbrains.kotlin.types.checker.ClassicTypeSystemContext import org.jetbrains.kotlin.types.model.StubTypeMarker import org.jetbrains.kotlin.types.model.TypeConstructorMarker import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker @@ -29,25 +30,27 @@ import org.jetbrains.kotlin.types.model.TypeSystemInferenceExtensionContext import org.jetbrains.kotlin.utils.addToStdlib.cast fun ConstraintStorage.buildCurrentSubstitutor(additionalBindings: Map): NewTypeSubstitutorByConstructorMap = - NewTypeSubstitutorByConstructorMap( (fixedTypeVariables.entries.associate { it.key to it.value } + additionalBindings).cast() ) // TODO: SUB + NewTypeSubstitutorByConstructorMap((fixedTypeVariables.entries.associate { it.key to it.value } + additionalBindings).cast()) // TODO: SUB -fun ConstraintStorage.buildResultingSubstitutor(context: TypeSystemInferenceExtensionContext): TypeSubstitutorMarker = with(context) { - val currentSubstitutorMap = fixedTypeVariables.entries.associate { - it.key to it.value - } - val uninferredSubstitutorMap = notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> - freshTypeConstructor to context.createErrorTypeWithCustomConstructor( - "Uninferred type", - (typeVariable.typeVariable).freshTypeConstructor() - ) +fun ConstraintStorage.buildAbstractResultingSubstitutor(context: TypeSystemInferenceExtensionContext): TypeSubstitutorMarker = + with(context) { + val currentSubstitutorMap = fixedTypeVariables.entries.associate { + it.key to it.value + } + val uninferredSubstitutorMap = notFixedTypeVariables.entries.associate { (freshTypeConstructor, typeVariable) -> + freshTypeConstructor to context.createErrorTypeWithCustomConstructor( + "Uninferred type", + (typeVariable.typeVariable).freshTypeConstructor() + ) + } + + return context.typeSubstitutorByTypeConstructor(currentSubstitutorMap + uninferredSubstitutorMap) } - return context.typeSubstitutorByTypeConstructor(currentSubstitutorMap + uninferredSubstitutorMap) +fun ConstraintStorage.buildResultingSubstitutor(context: TypeSystemInferenceExtensionContext): NewTypeSubstitutor { + return buildAbstractResultingSubstitutor(context) as NewTypeSubstitutor } -fun ConstraintStorage.buildResultingSubstitutor(): NewTypeSubstitutor = - buildResultingSubstitutor(this as TypeSystemInferenceExtensionContext) as NewTypeSubstitutor - val CallableDescriptor.returnTypeOrNothing: UnwrappedType get() { returnType?.let { return it.unwrap() } @@ -72,7 +75,7 @@ fun CallableDescriptor.substituteAndApproximateCapturedTypes(substitutor: NewTyp override fun prepareTopLevelType(topLevelType: KotlinType, position: Variance) = substitutor.safeSubstitute(topLevelType.unwrap()).let { substitutedType -> TypeApproximator(builtIns).approximateToSuperType(substitutedType, TypeApproximatorConfiguration.CapturedAndIntegerLiteralsTypesApproximation) - ?: substitutedType + ?: substitutedType } }