Minor, move some utilities closer to their usages

This commit is contained in:
Alexander Udalov
2015-10-23 20:28:36 +03:00
parent d6e87c50ab
commit 302cd3ca2f
2 changed files with 25 additions and 30 deletions
@@ -114,7 +114,7 @@ public class TypeBoundsImpl(
//fun <T> foo(t: T, consumer: Consumer<T>): T
//foo(1, c: Consumer<Any>) - infer Int, not Any here
val superTypeOfNumberLowerBounds = TypeUtils.commonSupertypeForNumberTypes(numberLowerBounds)
val superTypeOfNumberLowerBounds = commonSupertypeForNumberTypes(numberLowerBounds)
if (tryPossibleAnswer(bounds, superTypeOfNumberLowerBounds)) {
return setOf(superTypeOfNumberLowerBounds!!)
}
@@ -152,7 +152,7 @@ public class TypeBoundsImpl(
// For non-denotable number types only, no valid types are mentioned, so common supertype is valid
val numberLowerBounds = filterBounds(bounds, LOWER_BOUND).filter { it.constructor is IntegerValueTypeConstructor }
val superTypeOfNumberLowerBounds = TypeUtils.commonSupertypeForNumberTypes(numberLowerBounds)
val superTypeOfNumberLowerBounds = commonSupertypeForNumberTypes(numberLowerBounds)
if (possibleAnswer == superTypeOfNumberLowerBounds) return true
return false
@@ -182,4 +182,25 @@ public class TypeBoundsImpl(
}
return true
}
}
private fun commonSupertypeForNumberTypes(numberLowerBounds: Collection<KotlinType>): KotlinType? {
if (numberLowerBounds.isEmpty()) return null
val intersectionOfSupertypes = getIntersectionOfSupertypes(numberLowerBounds)
return TypeUtils.getDefaultPrimitiveNumberType(intersectionOfSupertypes) ?:
CommonSupertypes.commonSupertype(numberLowerBounds)
}
private fun getIntersectionOfSupertypes(types: Collection<KotlinType>): Set<KotlinType> {
val upperBounds = HashSet<KotlinType>()
for (type in types) {
val supertypes = type.constructor.supertypes
if (upperBounds.isEmpty()) {
upperBounds.addAll(supertypes)
}
else {
upperBounds.retainAll(supertypes)
}
}
return upperBounds
}
}
@@ -447,32 +447,6 @@ public class TypeUtils {
return new StarProjectionImpl(parameterDescriptor);
}
@Nullable
public static KotlinType commonSupertypeForNumberTypes(@NotNull Collection<KotlinType> numberLowerBounds) {
if (numberLowerBounds.isEmpty()) return null;
Set<KotlinType> intersectionOfSupertypes = getIntersectionOfSupertypes(numberLowerBounds);
KotlinType primitiveNumberType = getDefaultPrimitiveNumberType(intersectionOfSupertypes);
if (primitiveNumberType != null) {
return primitiveNumberType;
}
return CommonSupertypes.commonSupertype(numberLowerBounds);
}
@NotNull
private static Set<KotlinType> getIntersectionOfSupertypes(@NotNull Collection<KotlinType> types) {
Set<KotlinType> upperBounds = new HashSet<KotlinType>();
for (KotlinType type : types) {
Collection<KotlinType> supertypes = type.getConstructor().getSupertypes();
if (upperBounds.isEmpty()) {
upperBounds.addAll(supertypes);
}
else {
upperBounds.retainAll(supertypes);
}
}
return upperBounds;
}
@NotNull
public static KotlinType getDefaultPrimitiveNumberType(@NotNull IntegerValueTypeConstructor numberValueTypeConstructor) {
KotlinType type = getDefaultPrimitiveNumberType(numberValueTypeConstructor.getSupertypes());
@@ -482,7 +456,7 @@ public class TypeUtils {
}
@Nullable
private static KotlinType getDefaultPrimitiveNumberType(@NotNull Collection<KotlinType> supertypes) {
public static KotlinType getDefaultPrimitiveNumberType(@NotNull Collection<KotlinType> supertypes) {
if (supertypes.isEmpty()) {
return null;
}