From 26a54aa95dd079bd9d09ac9f3e59c92a2acab677 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Mon, 21 Jul 2014 20:08:51 +0400 Subject: [PATCH] Small refactoring in ConstraintSystemImpl Extracted isMyTypeVariable, getMyTypeVariable --- .../calls/inference/ConstraintSystem.kt | 2 +- .../calls/inference/ConstraintSystemImpl.kt | 38 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.kt index 383143e502c..bef4df56ee5 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystem.kt @@ -56,7 +56,7 @@ public trait ConstraintSystem { /** * Returns the resulting type constraints of solving the constraint system for specific type variable.

- * Returns null if the type variable was not registered. + * Throws IllegalArgumentException if the type variable was not registered. */ public fun getTypeBounds(typeVariable: TypeParameterDescriptor): TypeBounds diff --git a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt index 397365c909b..f95464009f6 100644 --- a/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt +++ b/core/descriptors/src/org/jetbrains/jet/lang/resolve/calls/inference/ConstraintSystemImpl.kt @@ -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()