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 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 public fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBounds
@@ -312,7 +312,7 @@ public class ConstraintSystemImpl : ConstraintSystem {
boundKind: TypeBounds.BoundKind, boundKind: TypeBounds.BoundKind,
constraintPosition: ConstraintPosition constraintPosition: ConstraintPosition
) { ) {
val typeBounds = getTypeBounds(parameterType).sure("constraint should be generated only for type variables") val typeBounds = getTypeBounds(parameterType)
if (!parameterType.isMarkedNullable() || !constrainingType.isMarkedNullable()) { if (!parameterType.isMarkedNullable() || !constrainingType.isMarkedNullable()) {
typeBounds.addBound(boundKind, constrainingType, constraintPosition) typeBounds.addBound(boundKind, constrainingType, constraintPosition)
@@ -343,10 +343,9 @@ public class ConstraintSystemImpl : ConstraintSystem {
addSubtypeConstraint(bound.constrainingType, declaredUpperBound, position) addSubtypeConstraint(bound.constrainingType, declaredUpperBound, position)
} }
} }
val declarationDescriptor = declaredUpperBound.getConstructor().getDeclarationDescriptor() if (isMyTypeVariable(declaredUpperBound)) {
if (declarationDescriptor is TypeParameterDescriptor && typeParameterBounds.containsKey(declarationDescriptor)) { val typeBoundsForUpperBound = getTypeBounds(declaredUpperBound)
val typeBoundsForUpperBound = typeParameterBounds.get(declarationDescriptor) for (bound in typeBoundsForUpperBound.bounds) {
for (bound in typeBoundsForUpperBound!!.bounds) {
if (bound.kind == UPPER_BOUND || bound.kind == EXACT_BOUND) { if (bound.kind == UPPER_BOUND || bound.kind == EXACT_BOUND) {
val position = getCompoundConstraintPosition( val position = getCompoundConstraintPosition(
TYPE_BOUND_POSITION.position(typeParameterDescriptor.getIndex()), bound.position) TYPE_BOUND_POSITION.position(typeParameterDescriptor.getIndex()), bound.position)
@@ -360,22 +359,25 @@ public class ConstraintSystemImpl : ConstraintSystem {
override fun getTypeVariables() = typeParameterBounds.keySet() override fun getTypeVariables() = typeParameterBounds.keySet()
override fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBounds { override fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBoundsImpl {
return typeParameterBounds.get(typeVariable).sure( if (!isMyTypeVariable(typeVariable)) {
"TypeParameterDescriptor is not a type variable for constraint system: $typeVariable") throw IllegalArgumentException("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)
} }
return null return typeParameterBounds[typeVariable]!!
} }
private fun isMyTypeVariable(type: JetType): Boolean { private fun getTypeBounds(parameterType: JetType): TypeBoundsImpl {
val descriptor = type.getConstructor().getDeclarationDescriptor() assert (isMyTypeVariable(parameterType)) { "Type is not a type variable for constraint system: $parameterType" }
return descriptor is TypeParameterDescriptor && typeParameterBounds.get(descriptor) != null 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() override fun getResultingSubstitutor() = replaceUninferredBySpecialErrorType()