From 36ea9484a9e681ff0d4ff733368d0b63bfda559f Mon Sep 17 00:00:00 2001 From: Stanislav Erokhin Date: Thu, 4 May 2017 15:55:00 +0300 Subject: [PATCH] [NI] Minor. Extract code to function. Also make KotlinCall unnecessary for TypeVariableFromCallableDescriptor. --- .../calls/components/ResolutionParts.kt | 53 +++++++++++-------- .../components/SimpleConstraintSystemImpl.kt | 17 +----- .../calls/inference/model/TypeVariable.kt | 4 +- 3 files changed, 35 insertions(+), 39 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 4b0485b348d..5960748d6c2 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.ConstraintSystemOperation 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 @@ -108,26 +109,8 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { descriptorWithFreshTypes = candidateDescriptor return emptyList() } - val typeParameters = candidateDescriptor.typeParameters - - val freshTypeVariables = typeParameters.map { TypeVariableFromCallableDescriptor(kotlinCall, it) } - typeVariablesForFreshTypeParameters = freshTypeVariables - - val toFreshVariables = FreshVariableNewTypeSubstitutor(freshTypeVariables) - - for (freshVariable in freshTypeVariables) { - csBuilder.registerVariable(freshVariable) - } - - for (index in typeParameters.indices) { - val typeParameter = typeParameters[index] - val freshVariable = freshTypeVariables[index] - val position = DeclaredUpperBoundConstraintPosition(typeParameter) - - for (upperBound in typeParameter.upperBounds) { - csBuilder.addSubtypeConstraint(freshVariable.defaultType, toFreshVariables.safeSubstitute(upperBound.unwrap()), position) - } - } + val toFreshVariables = createToFreshVariableSubstitutorAndAddInitialConstraints(candidateDescriptor, csBuilder, kotlinCall) + typeVariablesForFreshTypeParameters = toFreshVariables.freshVariables // bad function -- error on declaration side if (csBuilder.hasContradiction) { @@ -142,12 +125,13 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { return emptyList() } + val typeParameters = candidateDescriptor.typeParameters for (index in typeParameters.indices) { val typeParameter = typeParameters[index] val typeArgument = typeArgumentMappingByOriginal.getTypeArgument(typeParameter) if (typeArgument is SimpleTypeArgument) { - val freshVariable = freshTypeVariables[index] + val freshVariable = toFreshVariables.freshVariables[index] csBuilder.addEqualityConstraint(freshVariable.defaultType, typeArgument.type, ExplicitTypeParameterConstraintPosition(typeArgument)) } else { @@ -170,6 +154,33 @@ internal object CreateDescriptorWithFreshTypeVariables : ResolutionPart { return emptyList() } + + fun createToFreshVariableSubstitutorAndAddInitialConstraints( + candidateDescriptor: CallableDescriptor, + csBuilder: ConstraintSystemOperation, + kotlinCall: KotlinCall? + ): FreshVariableNewTypeSubstitutor { + val typeParameters = candidateDescriptor.typeParameters + + val freshTypeVariables = typeParameters.map { TypeVariableFromCallableDescriptor(it, kotlinCall) } + + val toFreshVariables = FreshVariableNewTypeSubstitutor(freshTypeVariables) + + for (freshVariable in freshTypeVariables) { + csBuilder.registerVariable(freshVariable) + } + + for (index in typeParameters.indices) { + val typeParameter = typeParameters[index] + val freshVariable = freshTypeVariables[index] + val position = DeclaredUpperBoundConstraintPosition(typeParameter) + + for (upperBound in typeParameter.upperBounds) { + csBuilder.addSubtypeConstraint(freshVariable.defaultType, toFreshVariables.safeSubstitute(upperBound.unwrap()), position) + } + } + return toFreshVariables + } } internal object CheckExplicitReceiverKindConsistency : ResolutionPart { diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt index a64f68fc908..5570d5612cf 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/components/SimpleConstraintSystemImpl.kt @@ -17,19 +17,16 @@ package org.jetbrains.kotlin.resolve.calls.inference.components import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemBuilder import org.jetbrains.kotlin.resolve.calls.inference.model.NewConstraintSystemImpl import org.jetbrains.kotlin.resolve.calls.inference.model.SimpleConstraintSystemConstraintPosition import org.jetbrains.kotlin.resolve.calls.inference.model.TypeVariableFromCallableDescriptor import org.jetbrains.kotlin.resolve.calls.inference.substitute -import org.jetbrains.kotlin.resolve.calls.model.* import org.jetbrains.kotlin.resolve.calls.results.SimpleConstraintSystem import org.jetbrains.kotlin.types.TypeConstructorSubstitution import org.jetbrains.kotlin.types.TypeSubstitutor import org.jetbrains.kotlin.types.UnwrappedType import org.jetbrains.kotlin.types.typeUtil.asTypeProjection -import java.lang.UnsupportedOperationException class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, resultTypeResolver: ResultTypeResolver) : SimpleConstraintSystem { @@ -37,7 +34,7 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, resultT override fun registerTypeVariables(typeParameters: Collection): TypeSubstitutor { val substitutionMap = typeParameters.associate { - val variable = TypeVariableFromCallableDescriptor(ThrowableKotlinCall, it) + val variable = TypeVariableFromCallableDescriptor(it) csBuilder.registerVariable(variable) it.defaultType.constructor to variable.defaultType.asTypeProjection() @@ -57,16 +54,4 @@ class SimpleConstraintSystemImpl(constraintInjector: ConstraintInjector, resultT override fun hasContradiction() = csBuilder.hasContradiction override val captureFromArgument get() = true - - private object ThrowableKotlinCall : KotlinCall { - override val callKind: KotlinCallKind get() = throw UnsupportedOperationException() - override val explicitReceiver: ReceiverKotlinCallArgument? get() = throw UnsupportedOperationException() - override val name: Name get() = throw UnsupportedOperationException() - override val typeArguments: List get() = throw UnsupportedOperationException() - override val argumentsInParenthesis: List get() = throw UnsupportedOperationException() - override val externalArgument: KotlinCallArgument? get() = throw UnsupportedOperationException() - override val isInfixCall: Boolean get() = throw UnsupportedOperationException() - override val isOperatorCall: Boolean get() = throw UnsupportedOperationException() - override val isSuperOrDelegatingConstructorCall: Boolean get() = throw UnsupportedOperationException() - } } \ No newline at end of file diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt index 9c0219fd7b4..88b0ac80809 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/inference/model/TypeVariable.kt @@ -50,8 +50,8 @@ sealed class NewTypeVariable(builtIns: KotlinBuiltIns, name: String) { } class TypeVariableFromCallableDescriptor( - val call: KotlinCall, - val originalTypeParameter: TypeParameterDescriptor + val originalTypeParameter: TypeParameterDescriptor, + val call: KotlinCall? = null ) : NewTypeVariable(originalTypeParameter.builtIns, originalTypeParameter.name.identifier) class LambdaTypeVariable(