Simplify signature of ConstraintSystem#registerTypeVariables
Drop unused variance from TypeBounds and ConstraintSystem, provide default values, inline methods used only once
This commit is contained in:
+3
-5
@@ -48,9 +48,7 @@ import org.jetbrains.kotlin.types.TypeUtils.DONT_CARE
|
|||||||
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||||
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
||||||
|
|
||||||
class GenericCandidateResolver(
|
class GenericCandidateResolver(private val argumentTypeResolver: ArgumentTypeResolver) {
|
||||||
private val argumentTypeResolver: ArgumentTypeResolver
|
|
||||||
) {
|
|
||||||
fun <D : CallableDescriptor> inferTypeArguments(context: CallCandidateResolutionContext<D>): ResolutionStatus {
|
fun <D : CallableDescriptor> inferTypeArguments(context: CallCandidateResolutionContext<D>): ResolutionStatus {
|
||||||
val candidateCall = context.candidateCall
|
val candidateCall = context.candidateCall
|
||||||
val candidate = candidateCall.candidateDescriptor
|
val candidate = candidateCall.candidateDescriptor
|
||||||
@@ -67,7 +65,7 @@ class GenericCandidateResolver(
|
|||||||
val candidateWithFreshVariables = FunctionDescriptorUtil.alphaConvertTypeParameters(candidate)
|
val candidateWithFreshVariables = FunctionDescriptorUtil.alphaConvertTypeParameters(candidate)
|
||||||
|
|
||||||
val conversionToOriginal = candidateWithFreshVariables.typeParameters.zip(candidate.typeParameters).toMap()
|
val conversionToOriginal = candidateWithFreshVariables.typeParameters.zip(candidate.typeParameters).toMap()
|
||||||
constraintSystem.registerTypeVariables(candidateWithFreshVariables.typeParameters, { Variance.INVARIANT }, { conversionToOriginal[it]!! })
|
constraintSystem.registerTypeVariables(candidateWithFreshVariables.typeParameters, { conversionToOriginal[it]!! })
|
||||||
|
|
||||||
val substituteDontCare = makeConstantSubstitutor(candidate.typeParameters, DONT_CARE)
|
val substituteDontCare = makeConstantSubstitutor(candidate.typeParameters, DONT_CARE)
|
||||||
|
|
||||||
@@ -166,7 +164,7 @@ class GenericCandidateResolver(
|
|||||||
val conversion = candidateDescriptor.typeParameters.zip(candidateWithFreshVariables.typeParameters).toMap()
|
val conversion = candidateDescriptor.typeParameters.zip(candidateWithFreshVariables.typeParameters).toMap()
|
||||||
|
|
||||||
val freshVariables = nestedTypeVariables.map { conversion[it] }.filterNotNull()
|
val freshVariables = nestedTypeVariables.map { conversion[it] }.filterNotNull()
|
||||||
constraintSystem.registerTypeVariables(freshVariables, { Variance.INVARIANT }, { it }, external = true)
|
constraintSystem.registerTypeVariables(freshVariables, external = true)
|
||||||
|
|
||||||
constraintSystem.addSubtypeConstraint(candidateWithFreshVariables.returnType, effectiveExpectedType, constraintPosition)
|
constraintSystem.addSubtypeConstraint(candidateWithFreshVariables.returnType, effectiveExpectedType, constraintPosition)
|
||||||
return true
|
return true
|
||||||
|
|||||||
+1
-3
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
|||||||
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition
|
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||||
import org.jetbrains.kotlin.types.Variance
|
|
||||||
|
|
||||||
public interface ConstraintSystem {
|
public interface ConstraintSystem {
|
||||||
/**
|
/**
|
||||||
@@ -29,8 +28,7 @@ public interface ConstraintSystem {
|
|||||||
*/
|
*/
|
||||||
public fun registerTypeVariables(
|
public fun registerTypeVariables(
|
||||||
typeVariables: Collection<TypeParameterDescriptor>,
|
typeVariables: Collection<TypeParameterDescriptor>,
|
||||||
variance: (TypeParameterDescriptor) -> Variance,
|
mapToOriginal: (TypeParameterDescriptor) -> TypeParameterDescriptor = { it },
|
||||||
mapToOriginal: (TypeParameterDescriptor) -> TypeParameterDescriptor,
|
|
||||||
external: Boolean = false
|
external: Boolean = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+2
-14
@@ -142,14 +142,13 @@ public class ConstraintSystemImpl : ConstraintSystem {
|
|||||||
|
|
||||||
override fun registerTypeVariables(
|
override fun registerTypeVariables(
|
||||||
typeVariables: Collection<TypeParameterDescriptor>,
|
typeVariables: Collection<TypeParameterDescriptor>,
|
||||||
variance: (TypeParameterDescriptor) -> Variance,
|
|
||||||
mapToOriginal: (TypeParameterDescriptor) -> TypeParameterDescriptor,
|
mapToOriginal: (TypeParameterDescriptor) -> TypeParameterDescriptor,
|
||||||
external: Boolean
|
external: Boolean
|
||||||
) {
|
) {
|
||||||
if (external) externalTypeParameters.addAll(typeVariables)
|
if (external) externalTypeParameters.addAll(typeVariables)
|
||||||
|
|
||||||
for (typeVariable in typeVariables) {
|
for (typeVariable in typeVariables) {
|
||||||
allTypeParameterBounds.put(typeVariable, TypeBoundsImpl(typeVariable, variance(typeVariable)))
|
allTypeParameterBounds.put(typeVariable, TypeBoundsImpl(typeVariable))
|
||||||
val original = mapToOriginal(typeVariable)
|
val original = mapToOriginal(typeVariable)
|
||||||
originalToVariables[original] = typeVariable
|
originalToVariables[original] = typeVariable
|
||||||
variablesToOriginal[typeVariable] = original
|
variablesToOriginal[typeVariable] = original
|
||||||
@@ -555,17 +554,6 @@ class SubstitutionFilteringInternalResolveAnnotations(substitution: TypeSubstitu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun ConstraintSystemImpl.registerTypeVariables(typeVariables: Map<TypeParameterDescriptor, Variance>) {
|
|
||||||
registerTypeVariables(typeVariables.keySet(), { typeVariables[it]!! }, { it })
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun ConstraintSystemImpl.registerTypeVariables(
|
|
||||||
typeVariables: Collection<TypeParameterDescriptor>,
|
|
||||||
variance: (TypeParameterDescriptor) -> Variance
|
|
||||||
) {
|
|
||||||
registerTypeVariables(typeVariables, variance, { it })
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun createTypeSubstitutor(conversion: (TypeParameterDescriptor) -> TypeParameterDescriptor?): TypeSubstitutor {
|
public fun createTypeSubstitutor(conversion: (TypeParameterDescriptor) -> TypeParameterDescriptor?): TypeSubstitutor {
|
||||||
return TypeSubstitutor.create(object : TypeConstructorSubstitution() {
|
return TypeSubstitutor.create(object : TypeConstructorSubstitution() {
|
||||||
override fun get(key: TypeConstructor): TypeProjection? {
|
override fun get(key: TypeConstructor): TypeProjection? {
|
||||||
@@ -577,4 +565,4 @@ public fun createTypeSubstitutor(conversion: (TypeParameterDescriptor) -> TypePa
|
|||||||
return TypeProjectionImpl(type)
|
return TypeProjectionImpl(type)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,17 +23,14 @@ import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.LOWER_B
|
|||||||
import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.UPPER_BOUND
|
import org.jetbrains.kotlin.resolve.calls.inference.TypeBounds.BoundKind.UPPER_BOUND
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition
|
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPosition
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.Variance
|
|
||||||
|
|
||||||
public interface TypeBounds {
|
public interface TypeBounds {
|
||||||
public val varianceOfPosition: Variance
|
|
||||||
|
|
||||||
public val typeVariable: TypeParameterDescriptor
|
public val typeVariable: TypeParameterDescriptor
|
||||||
|
|
||||||
public val bounds: Collection<Bound>
|
public val bounds: Collection<Bound>
|
||||||
|
|
||||||
public val value: KotlinType?
|
public val value: KotlinType?
|
||||||
get() = if (values.size() == 1) values.first() else null
|
get() = if (values.size == 1) values.first() else null
|
||||||
|
|
||||||
public val values: Collection<KotlinType>
|
public val values: Collection<KotlinType>
|
||||||
|
|
||||||
|
|||||||
+2
-5
@@ -28,10 +28,7 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
|||||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
public class TypeBoundsImpl(
|
public class TypeBoundsImpl(override val typeVariable: TypeParameterDescriptor) : TypeBounds {
|
||||||
override val typeVariable: TypeParameterDescriptor,
|
|
||||||
override val varianceOfPosition: Variance
|
|
||||||
) : TypeBounds {
|
|
||||||
override val bounds = ArrayList<Bound>()
|
override val bounds = ArrayList<Bound>()
|
||||||
|
|
||||||
private var resultValues: Collection<KotlinType>? = null
|
private var resultValues: Collection<KotlinType>? = null
|
||||||
@@ -67,7 +64,7 @@ public class TypeBoundsImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public fun filter(condition: (ConstraintPosition) -> Boolean): TypeBoundsImpl {
|
public fun filter(condition: (ConstraintPosition) -> Boolean): TypeBoundsImpl {
|
||||||
val result = TypeBoundsImpl(typeVariable, varianceOfPosition)
|
val result = TypeBoundsImpl(typeVariable)
|
||||||
result.bounds.addAll(bounds.filter { condition(it.position) })
|
result.bounds.addAll(bounds.filter { condition(it.position) })
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImplKt.registerTypeVariables;
|
|
||||||
import static org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.SPECIAL;
|
import static org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.SPECIAL;
|
||||||
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
|
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
|
||||||
|
|
||||||
@@ -186,7 +185,12 @@ public class TypeIntersector {
|
|||||||
processAllTypeParameters(withParameters, Variance.INVARIANT, processor);
|
processAllTypeParameters(withParameters, Variance.INVARIANT, processor);
|
||||||
processAllTypeParameters(expected, Variance.INVARIANT, processor);
|
processAllTypeParameters(expected, Variance.INVARIANT, processor);
|
||||||
ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl();
|
ConstraintSystemImpl constraintSystem = new ConstraintSystemImpl();
|
||||||
registerTypeVariables(constraintSystem, parameters);
|
constraintSystem.registerTypeVariables(parameters.keySet(), new Function1<TypeParameterDescriptor, TypeParameterDescriptor>() {
|
||||||
|
@Override
|
||||||
|
public TypeParameterDescriptor invoke(TypeParameterDescriptor descriptor) {
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
constraintSystem.addSubtypeConstraint(withParameters, expected, SPECIAL.position());
|
constraintSystem.addSubtypeConstraint(withParameters, expected, SPECIAL.position());
|
||||||
|
|
||||||
return constraintSystem.getStatus().isSuccessful();
|
return constraintSystem.getStatus().isSuccessful();
|
||||||
|
|||||||
+1
-3
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.resolve.TypeResolver
|
|||||||
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintContext
|
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintContext
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl
|
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.SPECIAL
|
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.SPECIAL
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.registerTypeVariables
|
|
||||||
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
|
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
|
||||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||||
import org.jetbrains.kotlin.test.KotlinLiteFixture
|
import org.jetbrains.kotlin.test.KotlinLiteFixture
|
||||||
@@ -33,7 +32,6 @@ import org.jetbrains.kotlin.types.ErrorUtils
|
|||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
abstract public class AbstractConstraintSystemTest() : KotlinLiteFixture() {
|
abstract public class AbstractConstraintSystemTest() : KotlinLiteFixture() {
|
||||||
|
|
||||||
@@ -83,7 +81,7 @@ abstract public class AbstractConstraintSystemTest() : KotlinLiteFixture() {
|
|||||||
val variables = parseVariables(constraintsFileText)
|
val variables = parseVariables(constraintsFileText)
|
||||||
val fixVariables = constraintsFileText.contains("FIX_VARIABLES")
|
val fixVariables = constraintsFileText.contains("FIX_VARIABLES")
|
||||||
val typeParameterDescriptors = variables.map { testDeclarations.getParameterDescriptor(it) }
|
val typeParameterDescriptors = variables.map { testDeclarations.getParameterDescriptor(it) }
|
||||||
constraintSystem.registerTypeVariables(typeParameterDescriptors, { Variance.INVARIANT })
|
constraintSystem.registerTypeVariables(typeParameterDescriptors)
|
||||||
|
|
||||||
val constraints = parseConstraints(constraintsFileText)
|
val constraints = parseConstraints(constraintsFileText)
|
||||||
fun KotlinType.assertNotError(): KotlinType {
|
fun KotlinType.assertNotError(): KotlinType {
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl
|
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystemImpl
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind
|
import org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind
|
||||||
import org.jetbrains.kotlin.resolve.calls.inference.registerTypeVariables
|
|
||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.TypeSubstitutor
|
import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
@@ -118,8 +117,8 @@ class FuzzyType(
|
|||||||
}
|
}
|
||||||
|
|
||||||
val constraintSystem = ConstraintSystemImpl()
|
val constraintSystem = ConstraintSystemImpl()
|
||||||
constraintSystem.registerTypeVariables(freeParameters, { Variance.INVARIANT })
|
constraintSystem.registerTypeVariables(freeParameters)
|
||||||
constraintSystem.registerTypeVariables(otherType.freeParameters, { Variance.INVARIANT })
|
constraintSystem.registerTypeVariables(otherType.freeParameters)
|
||||||
|
|
||||||
when (matchKind) {
|
when (matchKind) {
|
||||||
MatchKind.IS_SUBTYPE -> constraintSystem.addSubtypeConstraint(type, otherType.type, ConstraintPositionKind.RECEIVER_POSITION.position())
|
MatchKind.IS_SUBTYPE -> constraintSystem.addSubtypeConstraint(type, otherType.type, ConstraintPositionKind.RECEIVER_POSITION.position())
|
||||||
|
|||||||
Reference in New Issue
Block a user