[NI] Minor. Extract code to function.
Also make KotlinCall unnecessary for TypeVariableFromCallableDescriptor.
This commit is contained in:
committed by
Mikhail Zarechenskiy
parent
0b358fb693
commit
36ea9484a9
+32
-21
@@ -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 {
|
||||
|
||||
+1
-16
@@ -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<TypeParameterDescriptor>): 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<TypeArgument> get() = throw UnsupportedOperationException()
|
||||
override val argumentsInParenthesis: List<KotlinCallArgument> 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()
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user