Small refactoring in ConstraintSystemImpl

Extracted isMyTypeVariable, getMyTypeVariable
This commit is contained in:
Svetlana Isakova
2014-07-21 20:08:51 +04:00
parent a2fa38a7f9
commit 26a54aa95d
2 changed files with 21 additions and 19 deletions
@@ -56,7 +56,7 @@ public trait ConstraintSystem {
/**
* Returns the resulting type constraints of solving the constraint system for specific type variable. <p/>
* Returns null if the type variable was not registered.
* Throws IllegalArgumentException if the type variable was not registered.
*/
public fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBounds
@@ -312,7 +312,7 @@ public class ConstraintSystemImpl : ConstraintSystem {
boundKind: TypeBounds.BoundKind,
constraintPosition: ConstraintPosition
) {
val typeBounds = getTypeBounds(parameterType).sure("constraint should be generated only for type variables")
val typeBounds = getTypeBounds(parameterType)
if (!parameterType.isMarkedNullable() || !constrainingType.isMarkedNullable()) {
typeBounds.addBound(boundKind, constrainingType, constraintPosition)
@@ -343,10 +343,9 @@ public class ConstraintSystemImpl : ConstraintSystem {
addSubtypeConstraint(bound.constrainingType, declaredUpperBound, position)
}
}
val declarationDescriptor = declaredUpperBound.getConstructor().getDeclarationDescriptor()
if (declarationDescriptor is TypeParameterDescriptor && typeParameterBounds.containsKey(declarationDescriptor)) {
val typeBoundsForUpperBound = typeParameterBounds.get(declarationDescriptor)
for (bound in typeBoundsForUpperBound!!.bounds) {
if (isMyTypeVariable(declaredUpperBound)) {
val typeBoundsForUpperBound = getTypeBounds(declaredUpperBound)
for (bound in typeBoundsForUpperBound.bounds) {
if (bound.kind == UPPER_BOUND || bound.kind == EXACT_BOUND) {
val position = getCompoundConstraintPosition(
TYPE_BOUND_POSITION.position(typeParameterDescriptor.getIndex()), bound.position)
@@ -360,22 +359,25 @@ public class ConstraintSystemImpl : ConstraintSystem {
override fun getTypeVariables() = typeParameterBounds.keySet()
override fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBounds {
return typeParameterBounds.get(typeVariable).sure(
"TypeParameterDescriptor is not a type variable for constraint system: $typeVariable")
}
private fun getTypeBounds(type: JetType): TypeBoundsImpl? {
val parameterDescriptor = type.getConstructor().getDeclarationDescriptor()
if (parameterDescriptor is TypeParameterDescriptor) {
return typeParameterBounds.get(parameterDescriptor)
override fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBoundsImpl {
if (!isMyTypeVariable(typeVariable)) {
throw IllegalArgumentException("TypeParameterDescriptor is not a type variable for constraint system: $typeVariable")
}
return null
return typeParameterBounds[typeVariable]!!
}
private fun isMyTypeVariable(type: JetType): Boolean {
val descriptor = type.getConstructor().getDeclarationDescriptor()
return descriptor is TypeParameterDescriptor && typeParameterBounds.get(descriptor) != null
private fun getTypeBounds(parameterType: JetType): TypeBoundsImpl {
assert (isMyTypeVariable(parameterType)) { "Type is not a type variable for constraint system: $parameterType" }
return getTypeBounds(getMyTypeVariable(parameterType)!!)
}
private fun isMyTypeVariable(typeVariable: TypeParameterDescriptor) = typeParameterBounds.contains(typeVariable)
private fun isMyTypeVariable(type: JetType): Boolean = getMyTypeVariable(type) != null
private fun getMyTypeVariable(type: JetType): TypeParameterDescriptor? {
val typeParameterDescriptor = type.getConstructor().getDeclarationDescriptor() as? TypeParameterDescriptor
return if (typeParameterDescriptor != null && isMyTypeVariable(typeParameterDescriptor)) typeParameterDescriptor else null
}
override fun getResultingSubstitutor() = replaceUninferredBySpecialErrorType()