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 bfaf3ce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
14 changes: 11 additions & 3 deletions packages/safe-ds-lang/src/language/typing/safe-ds-type-checker.ts
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 @@ -262,7 +266,9 @@ export class SafeDsTypeChecker {
other: Type,
options: TypeCheckOptions,
): boolean {
if (other instanceof NamedTupleType) {
if (other instanceof ClassType) {
return other.declaration === this.builtinClasses.Any;
} else if (other instanceof NamedTupleType) {
return (
type.length === other.length &&
type.entries.every((typeEntry, i) => {
Expand All @@ -281,6 +287,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 @@ -103,6 +103,7 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
const enumVariantType2 = typeComputer.computeType(enumVariant2) as EnumVariantType;

return [
// Callable type to callable type
{
type1: callableType1,
type2: callableType1,
Expand Down Expand Up @@ -526,6 +527,22 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
type2: enumType1,
expected: false,
},
// Named tuple type to class type
{
type1: factory.createNamedTupleType(),
type2: classType1,
expected: false,
},
{
type1: factory.createNamedTupleType(),
type2: coreTypes.Any,
expected: true,
},
{
type1: factory.createNamedTupleType(),
type2: coreTypes.AnyOrNull,
expected: true,
},
// Named tuple type to named tuple type
{
type1: factory.createNamedTupleType(),
Expand Down Expand Up @@ -604,6 +621,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 +691,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 +1054,7 @@ const typeParameterTypes = async (): Promise<IsSubOrSupertypeOfTest[]> => {
{
type1: unresolved,
type2: unbounded,
expected: false,
expected: true,
},
{
type1: coreTypes.AnyOrNull,
Expand Down Expand Up @@ -1107,7 +1150,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 bfaf3ce

Please sign in to comment.