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
@@ -0,0 +1,41 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// FILE: p/Super.java
package p;
public interface Super {
}
// FILE: p/Sub.java
package p;
public interface Sub extends Super {}
// FILE: p/Other.java
package p;
import java.util.*;
public class Other {
public static Sub sub;
public static Collection<Sub> subs;
public static Collection<Super> supers;
}
// FILE: k.kt
import p.*
fun test() {
val col = if (1 < 2) Other.subs else Other.supers
col.foo()
}
fun <T: Super> Collection<T>.foo(): T = null!!
fun listOf<T>(t: T): List<T> = null!!
@@ -7742,6 +7742,11 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/platformTypes/methodCall"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("inferenceWithBound.kt")
public void testInferenceWithBound() throws Exception {
doTest("compiler/testData/diagnostics/tests/platformTypes/methodCall/inferenceWithBound.kt");
}
@TestMetadata("int.kt")
public void testInt() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/platformTypes/methodCall/int.kt");
@@ -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