Skip to content

Commit

Permalink
feat: isSubtypeOf always returns true if 1. op is Nothing or 2.…
Browse files Browse the repository at this point in the history
… op is `Any?`
  • Loading branch information
lars-reimann committed Feb 20, 2024
1 parent 9d6ce28 commit bf18e02
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ export class SafeDsTypeChecker {
* Checks whether {@link type} is a subtype of {@link other}.
*/
isSubtypeOf = (type: Type, other: Type, options: TypeCheckOptions = {}): boolean => {
if (type === UnknownType || other === UnknownType) {
if (type.equals(this.coreTypes.Nothing) || other.equals(this.coreTypes.AnyOrNull)) {
return true;
} else if (type === UnknownType || other === UnknownType) {
return false;
} else if (other instanceof TypeParameterType) {
}

if (other instanceof TypeParameterType) {
const otherUpperBound = this.typeComputer().computeUpperBound(other);
return this.isSubtypeOf(type, otherUpperBound, options);
} else if (other instanceof UnionType) {
Expand Down Expand Up @@ -281,6 +285,8 @@ export class SafeDsTypeChecker {
private staticTypeIsSubtypeOf(type: StaticType, other: Type, options: TypeCheckOptions): boolean {
if (other instanceof CallableType) {
return this.isSubtypeOf(this.associatedCallableTypeForStaticType(type), other, options);
} else if (other instanceof ClassType) {
return other.declaration === this.builtinClasses.Any;
} else {
return type.equals(other);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,22 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
type2: callableType12,
expected: true,
},
// Static type to class type
{
type1: factory.createStaticType(classType1),
type2: classType1,
expected: false,
},
{
type1: factory.createStaticType(classType1),
type2: coreTypes.Any,
expected: true,
},
{
type1: factory.createStaticType(classType1),
type2: coreTypes.AnyOrNull,
expected: true,
},
// Static type to static type
{
type1: factory.createStaticType(classType1),
Expand Down Expand Up @@ -658,6 +674,16 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
type2: UnknownType,
expected: false,
},
{
type1: coreTypes.Nothing,
type2: UnknownType,
expected: true,
},
{
type1: UnknownType,
type2: coreTypes.AnyOrNull,
expected: true,
},
];
};

Expand Down Expand Up @@ -1011,7 +1037,7 @@ const typeParameterTypes = async (): Promise<IsSubOrSupertypeOfTest[]> => {
{
type1: unresolved,
type2: unbounded,
expected: false,
expected: true,
},
{
type1: coreTypes.AnyOrNull,
Expand Down Expand Up @@ -1107,7 +1133,7 @@ const typeParameterTypes = async (): Promise<IsSubOrSupertypeOfTest[]> => {
{
type1: coreTypes.Nothing,
type2: unresolved,
expected: false,
expected: true,
},

// Compare to some other type
Expand Down

0 comments on commit bf18e02

Please sign in to comment.