Nullable and Nothing supported

This commit is contained in:
Andrey Breslav
2011-01-27 16:47:22 +03:00
parent 820563a283
commit c7c28ac2e7
3 changed files with 40 additions and 5 deletions
@@ -8,12 +8,12 @@ import java.util.*;
* @author abreslav * @author abreslav
*/ */
public class JetStandardClasses { public class JetStandardClasses {
private static final TypeConstructor NOTHING = new TypeConstructor(
private static ClassDescriptor NOTHING_CLASS = new ClassDescriptor(
Collections.<Attribute>emptyList(), Collections.<Attribute>emptyList(),
"Nothing", "Nothing",
Collections.<TypeParameterDescriptor>emptyList(), Collections.<TypeParameterDescriptor>emptyList(),
new AbstractCollection<Type>() { new AbstractCollection<Type>() {
@Override @Override
public boolean contains(Object o) { public boolean contains(Object o) {
return o instanceof Type; return o instanceof Type;
@@ -28,7 +28,8 @@ public class JetStandardClasses {
public int size() { public int size() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}); }
);
private static final ClassDescriptor ANY = new ClassDescriptor( private static final ClassDescriptor ANY = new ClassDescriptor(
Collections.<Attribute>emptyList(), Collections.<Attribute>emptyList(),
@@ -85,8 +86,13 @@ public class JetStandardClasses {
private static final Type BOOLEAN_TYPE = new TypeImpl(getBoolean().getTypeConstructor(), STUB); private static final Type BOOLEAN_TYPE = new TypeImpl(getBoolean().getTypeConstructor(), STUB);
private static final Type STRING_TYPE = new TypeImpl(getString().getTypeConstructor(), STUB); private static final Type STRING_TYPE = new TypeImpl(getString().getTypeConstructor(), STUB);
private static final Type UNIT_TYPE = new TypeImpl(getTuple(0).getTypeConstructor(), STUB); private static final Type UNIT_TYPE = new TypeImpl(getTuple(0).getTypeConstructor(), STUB);
private static final Type NOTHING_TYPE = new TypeImpl(NOTHING, STUB); private static final Type NOTHING_TYPE = new TypeImpl(getNothing().getTypeConstructor(), STUB);
private static final Type NULLABLE_NOTHING_TYPE = new TypeImpl(Collections.<Attribute>emptyList(), NOTHING, true, Collections.<TypeProjection>emptyList(), TypeMemberDomain.EMPTY); private static final Type NULLABLE_NOTHING_TYPE = new TypeImpl(
Collections.<Attribute>emptyList(),
getNothing().getTypeConstructor(),
true,
Collections.<TypeProjection>emptyList(),
TypeMemberDomain.EMPTY);
@NotNull @NotNull
public static ClassDescriptor getAny() { public static ClassDescriptor getAny() {
@@ -143,6 +149,11 @@ public class JetStandardClasses {
return STRING; return STRING;
} }
@NotNull
public static ClassDescriptor getNothing() {
return NOTHING_CLASS;
}
@NotNull @NotNull
public static ClassDescriptor getTuple(int size) { public static ClassDescriptor getTuple(int size) {
return TUPLE[size]; return TUPLE[size];
@@ -75,6 +75,9 @@ public class JetTypeChecker {
if (!supertype.isNullable() && subtype.isNullable()) { if (!supertype.isNullable() && subtype.isNullable()) {
return false; return false;
} }
if (subtype.getConstructor() == JetStandardClasses.getNothing().getTypeConstructor()) {
return true;
}
@Nullable Type closestSupertype = findCorrespondingSupertype(subtype, supertype); @Nullable Type closestSupertype = findCorrespondingSupertype(subtype, supertype);
if (closestSupertype == null) { if (closestSupertype == null) {
return false; return false;
@@ -177,6 +177,25 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
assertSubtype("Derived_T<Any>", "Base_T<in Int>"); assertSubtype("Derived_T<Any>", "Base_T<in Int>");
} }
public void testNullable() throws Exception {
assertSubtype("Any?", "Any?");
assertSubtype("Any", "Any?");
assertNotSubtype("Any?", "Any");
assertSubtype("Int", "Any?");
assertSubtype("Int?", "Any?");
assertNotSubtype("Int?", "Any");
}
public void testNothing() throws Exception {
assertSubtype("Nothing", "Any");
assertSubtype("Nothing?", "Any?");
assertNotSubtype("Nothing?", "Any");
assertSubtype("Nothing", "Int");
assertSubtype("Nothing?", "Int?");
assertNotSubtype("Nothing?", "Int");
}
public void testImplicitConversions() throws Exception { public void testImplicitConversions() throws Exception {
assertConvertibleTo("1", JetStandardClasses.getByteType()); assertConvertibleTo("1", JetStandardClasses.getByteType());
} }
@@ -254,6 +273,8 @@ public class JetTypeCheckerTest extends LightDaemonAnalyzerTestCase {
return JetStandardClasses.getTuple(0); return JetStandardClasses.getTuple(0);
} else if ("Any".equals(name)) { } else if ("Any".equals(name)) {
return JetStandardClasses.getAny(); return JetStandardClasses.getAny();
} else if ("Nothing".equals(name)) {
return JetStandardClasses.getNothing();
} }
if (CLASSES.isEmpty()) { if (CLASSES.isEmpty()) {
for (String classDeclaration : CLASS_DECLARATIONS) { for (String classDeclaration : CLASS_DECLARATIONS) {