diff --git a/packages/safe-ds-lang/src/language/typing/safe-ds-type-checker.ts b/packages/safe-ds-lang/src/language/typing/safe-ds-type-checker.ts index dfc271efd..e9c55d86b 100644 --- a/packages/safe-ds-lang/src/language/typing/safe-ds-type-checker.ts +++ b/packages/safe-ds-lang/src/language/typing/safe-ds-type-checker.ts @@ -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) { @@ -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); } diff --git a/packages/safe-ds-lang/tests/language/typing/type checker/isSubOrSupertypeOf.test.ts b/packages/safe-ds-lang/tests/language/typing/type checker/isSubOrSupertypeOf.test.ts index 6a7d511ab..bb1362dea 100644 --- a/packages/safe-ds-lang/tests/language/typing/type checker/isSubOrSupertypeOf.test.ts +++ b/packages/safe-ds-lang/tests/language/typing/type checker/isSubOrSupertypeOf.test.ts @@ -604,6 +604,22 @@ const basic = async (): Promise => { 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), @@ -658,6 +674,16 @@ const basic = async (): Promise => { type2: UnknownType, expected: false, }, + { + type1: coreTypes.Nothing, + type2: UnknownType, + expected: true, + }, + { + type1: UnknownType, + type2: coreTypes.AnyOrNull, + expected: true, + }, ]; }; @@ -1011,7 +1037,7 @@ const typeParameterTypes = async (): Promise => { { type1: unresolved, type2: unbounded, - expected: false, + expected: true, }, { type1: coreTypes.AnyOrNull, @@ -1107,7 +1133,7 @@ const typeParameterTypes = async (): Promise => { { type1: coreTypes.Nothing, type2: unresolved, - expected: false, + expected: true, }, // Compare to some other type