CommonSupertypes support for flexible types

This commit is contained in:
Andrey Breslav
2014-08-25 12:15:31 +04:00
parent 583694a450
commit e232697da1
5 changed files with 74 additions and 5 deletions
@@ -47,6 +47,28 @@ public class CommonSupertypes {
@NotNull
public static JetType commonSupertype(@NotNull Collection<JetType> types) {
boolean hasFlexible = false;
List<JetType> upper = new ArrayList<JetType>(types.size());
List<JetType> lower = new ArrayList<JetType>(types.size());
for (JetType type : types) {
if (TypesPackage.isFlexible(type)) {
hasFlexible = true;
FlexibleType flexibleType = (FlexibleType) type;
upper.add(flexibleType.getUpperBound());
lower.add(flexibleType.getLowerBound());
}
else {
upper.add(type);
lower.add(type);
}
}
if (!hasFlexible) return commonSuperTypeForInflexible(types);
return new DelegatingFlexibleType(commonSuperTypeForInflexible(lower), commonSuperTypeForInflexible(upper));
}
@NotNull
private static JetType commonSuperTypeForInflexible(@NotNull Collection<JetType> types) {
assert !types.isEmpty();
Collection<JetType> typeSet = new HashSet<JetType>(types);
if (typeSet.size() == 1) return typeSet.iterator().next();
@@ -57,6 +79,7 @@ public class CommonSupertypes {
for (Iterator<JetType> iterator = typeSet.iterator(); iterator.hasNext();) {
JetType type = iterator.next();
assert type != null;
assert !TypesPackage.isFlexible(type) : "Flexible type " + type + " passed to commonSuperTypeForInflexible";
if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(type)) {
iterator.remove();
}
@@ -22,14 +22,14 @@ import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
// even if it turns out that the type an instance represents is not actually a type variable
// (i.e. it is not derived from a type parameter), see isTypeVariable
public trait CustomTypeVariable : JetType {
val isTypeVariable: Boolean
public val isTypeVariable: Boolean
// If typeParameterDescriptor != null <=> isTypeVariable == true, this is not a type variable
val typeParameterDescriptor: TypeParameterDescriptor?
public val typeParameterDescriptor: TypeParameterDescriptor?
// Throws an exception when isTypeVariable == false
fun substitutionResult(replacement: JetType): JetType
public fun substitutionResult(replacement: JetType): JetType
}
fun JetType.isCustomTypeVariable() = (this as? CustomTypeVariable)?.isTypeVariable ?: false
@@ -17,8 +17,8 @@
package org.jetbrains.jet.lang.types
public trait FlexibleType : JetType {
val lowerBound: JetType
val upperBound: JetType
public val lowerBound: JetType
public val upperBound: JetType
}
public fun JetType.isFlexible(): Boolean = this is FlexibleType