From 2602216039d14348dec994d2e0799bdf11292f64 Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Thu, 4 May 2017 15:46:52 +0300 Subject: [PATCH] [NI] Use NewTypeSubstitutor when create descriptor with fresh variables. --- .../resolve/calls/components/ResolutionParts.kt | 14 +++++++------- .../inference/components/NewTypeSubstitutor.kt | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt index 932331bab64..e4feb509648 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/components/ResolutionParts.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.resolve.calls.components import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.resolve.calls.components.TypeArgumentsToParametersMapper.TypeArgumentsMapping.NoExplicitArguments +import org.jetbrains.kotlin.resolve.calls.inference.components.FreshVariableNewTypeSubstitutor import org.jetbrains.kotlin.resolve.calls.inference.model.DeclaredUpperBoundConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.ExplicitTypeParameterConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor @@ -26,13 +27,12 @@ import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.smartcasts.getReceiverValueWithSmartCast import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind.* import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability -import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.* +import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.IMPOSSIBLE_TO_GENERATE +import org.jetbrains.kotlin.resolve.calls.tower.ResolutionCandidateApplicability.RUNTIME_ERROR import org.jetbrains.kotlin.resolve.calls.tower.VisibilityError -import org.jetbrains.kotlin.types.IndexedParametersSubstitution import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.Variance -import org.jetbrains.kotlin.types.typeUtil.asTypeProjection internal object CheckInstantiationOfAbstractClass : ResolutionPart { override fun SimpleKotlinResolutionCandidate.process(): List { @@ -112,8 +112,8 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { val freshTypeVariables = typeParameters.map { TypeVariableFromCallableDescriptor(kotlinCall, it) } typeVariablesForFreshTypeParameters = freshTypeVariables - val toFreshVariables = IndexedParametersSubstitution(typeParameters, - freshTypeVariables.map { it.defaultType.asTypeProjection() }).buildSubstitutor() + + val toFreshVariables = FreshVariableNewTypeSubstitutor(freshTypeVariables) for (freshVariable in freshTypeVariables) { csBuilder.registerVariable(freshVariable) @@ -125,7 +125,7 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { val position = DeclaredUpperBoundConstraintPosition(typeParameter) for (upperBound in typeParameter.upperBounds) { - csBuilder.addSubtypeConstraint(freshVariable.defaultType, upperBound.unwrap().substitute(toFreshVariables), position) + csBuilder.addSubtypeConstraint(freshVariable.defaultType, toFreshVariables.safeSubstitute(upperBound.unwrap()), position) } } @@ -137,7 +137,7 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { // optimization if (typeArgumentMappingByOriginal == NoExplicitArguments) { - descriptorWithFreshTypes = candidateDescriptor.safeSubstitute(toFreshVariables) + descriptorWithFreshTypes = candidateDescriptor.substitute(toFreshVariables)!! csBuilder.simplify().let { assert(it.isEmpty) { "Substitutor should be empty: $it, call: $kotlinCall" } } return emptyList() } diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt index 33cd7711ad7..9c477b013cc 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/NewTypeSubstitutor.kt @@ -16,6 +16,8 @@ package org.jetbrains.kotlin.resolve.calls.inference.components +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.checker.NewCapturedType import org.jetbrains.kotlin.types.checker.NewCapturedTypeConstructor @@ -120,4 +122,16 @@ interface NewTypeSubstitutor { class NewTypeSubstitutorByConstructorMap(val map: Map) : NewTypeSubstitutor { override val isEmpty get() = map.isEmpty() override fun substituteNotNullTypeWithConstructor(constructor: TypeConstructor): UnwrappedType? = map[constructor] +} + +class FreshVariableNewTypeSubstitutor(val freshVariables: List) : NewTypeSubstitutor { + override val isEmpty get() = freshVariables.isEmpty() + + override fun substituteNotNullTypeWithConstructor(constructor: TypeConstructor): UnwrappedType? { + val indexProposal = (constructor.declarationDescriptor as? TypeParameterDescriptor)?.index ?: return null + val typeVariable = freshVariables.getOrNull(indexProposal) ?: return null + if (typeVariable.originalTypeParameter.typeConstructor != constructor) return null + + return typeVariable.defaultType + } } \ No newline at end of file