Tests for common supertype inference
This commit is contained in:
+1
@@ -1,6 +1,7 @@
|
|||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<settings>
|
<settings>
|
||||||
<option name="PROJECT_PROFILE" value="idea.default" />
|
<option name="PROJECT_PROFILE" value="idea.default" />
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="true" />
|
||||||
<version value="1.0" />
|
<version value="1.0" />
|
||||||
</settings>
|
</settings>
|
||||||
</component>
|
</component>
|
||||||
@@ -150,7 +150,7 @@ public class JetTypeChecker {
|
|||||||
return result[0];
|
return result[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private Type commonSupertype(Collection<Type> types) {
|
public Type commonSupertype(Collection<Type> types) {
|
||||||
Collection<Type> typeSet = new HashSet<Type>(types);
|
Collection<Type> typeSet = new HashSet<Type>(types);
|
||||||
assert !typeSet.isEmpty();
|
assert !typeSet.isEmpty();
|
||||||
boolean nullable = false;
|
boolean nullable = false;
|
||||||
|
|||||||
@@ -59,8 +59,9 @@ public class TypeUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean canHaveSubtypes(Type type) {
|
private static boolean canHaveSubtypes(Type type) {
|
||||||
// TODO : a nullable type can have a subtype -- a non-nullable version
|
if (type.isNullable()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (!type.getConstructor().isSealed()) {
|
if (!type.getConstructor().isSealed()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,17 +87,30 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
|
|||||||
assertType("if (true) null else null", "Nothing?");
|
assertType("if (true) null else null", "Nothing?");
|
||||||
|
|
||||||
assertType("if (true) 1 else '1'", "Any");
|
assertType("if (true) 1 else '1'", "Any");
|
||||||
|
}
|
||||||
|
|
||||||
assertType("if (true) null : Base_T<*>? else null : Derived_T<*>?", "Base_T<*>?");
|
public void testCommonSupertypes() throws Exception {
|
||||||
assertType("if (true) null : Base_inT<*>? else null : Derived_T<*>?", "Any?");
|
assertCommonSupertype("Int", "Int", "Int");
|
||||||
assertType("if (true) null : DDerived_T<Int>? else null : Derived_T<Int>?", "Derived_T<Int>?");
|
|
||||||
assertType("if (true) null : DDerived_T<Int>? else null : DDerived1_T<Int>?", "Derived_T<Int>?");
|
|
||||||
|
|
||||||
assertType("if (true) null : Base_T<Int>? else null : Base_T<Boolean>?", "Base_T<out Any>?");
|
assertCommonSupertype("Int", "Int", "Nothing");
|
||||||
assertType("if (true) null : Base_T<Int>? else null : Base_T<in Int>?", "Base_T<in Int>?");
|
assertCommonSupertype("Int", "Nothing", "Int");
|
||||||
assertType("if (true) null : Derived_T<Int>? else null : Base_T<in Int>?", "Base_T<in Int>?");
|
assertCommonSupertype("Nothing", "Nothing", "Nothing");
|
||||||
assertType("if (true) null : Derived_T<in Int>? else null : Base_T<Int>?", "Base_T<in Int>?");
|
|
||||||
assertType("if (true) null : Base_T<Int>? else null : Base_T<*>?", "Base_T<*>?");
|
assertCommonSupertype("Int?", "Int", "Nothing?");
|
||||||
|
assertCommonSupertype("Nothing?", "Nothing?", "Nothing?");
|
||||||
|
|
||||||
|
assertCommonSupertype("Any", "Int", "Char");
|
||||||
|
|
||||||
|
assertCommonSupertype("Base_T<*>", "Base_T<*>", "Derived_T<*>");
|
||||||
|
assertCommonSupertype("Any", "Base_inT<*>", "Derived_T<*>");
|
||||||
|
assertCommonSupertype("Derived_T<Int>", "DDerived_T<Int>", "Derived_T<Int>");
|
||||||
|
assertCommonSupertype("Derived_T<Int>", "DDerived_T<Int>", "DDerived1_T<Int>");
|
||||||
|
|
||||||
|
assertCommonSupertype("Base_T<out Any>", "Base_T<Int>", "Base_T<Boolean>");
|
||||||
|
assertCommonSupertype("Base_T<in Int>", "Base_T<Int>", "Base_T<in Int>");
|
||||||
|
assertCommonSupertype("Base_T<in Int>", "Derived_T<Int>", "Base_T<in Int>");
|
||||||
|
assertCommonSupertype("Base_T<in Int>", "Derived_T<in Int>", "Base_T<Int>");
|
||||||
|
assertCommonSupertype("Base_T<*>", "Base_T<Int>", "Base_T<*>");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testBasicSubtyping() throws Exception {
|
public void testBasicSubtyping() throws Exception {
|
||||||
@@ -246,6 +259,15 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
|
|||||||
assertSubtypingRelation(type1, type2, false);
|
assertSubtypingRelation(type1, type2, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertCommonSupertype(String expected, String... types) {
|
||||||
|
Collection<Type> subtypes = new ArrayList<Type>();
|
||||||
|
for (String type : types) {
|
||||||
|
subtypes.add(makeType(type));
|
||||||
|
}
|
||||||
|
Type result = JetTypeChecker.INSTANCE.commonSupertype(subtypes);
|
||||||
|
assertTrue(result + " != " + expected, JetTypeChecker.INSTANCE.equalTypes(result, makeType(expected)));
|
||||||
|
}
|
||||||
|
|
||||||
private static void assertSubtypingRelation(String type1, String type2, boolean expected) {
|
private static void assertSubtypingRelation(String type1, String type2, boolean expected) {
|
||||||
Type typeNode1 = makeType(type1);
|
Type typeNode1 = makeType(type1);
|
||||||
Type typeNode2 = makeType(type2);
|
Type typeNode2 = makeType(type2);
|
||||||
|
|||||||
Reference in New Issue
Block a user