Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: substitute type parameters when checking assignability of overriding members #865

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/safe-ds-lang/src/language/validation/inheritance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expandToStringWithNL, getContainerOfType, ValidationAcceptor } from 'langium';
import { expandToStringWithNL, getContainerOfType, stream, ValidationAcceptor } from 'langium';
import { isEmpty, isEqualSet } from '../../helpers/collections.js';
import { isSdsClass, isSdsFunction, SdsClass, type SdsClassMember } from '../generated/ast.js';
import { getParentTypes, getQualifiedName } from '../helpers/nodeProperties.js';
Expand All @@ -18,13 +18,32 @@ export const classMemberMustMatchOverriddenMemberAndShouldBeNeeded = (services:
const typeComputer = services.types.TypeComputer;

return (node: SdsClassMember, accept: ValidationAcceptor): void => {
// Check whether the member overrides something
const overriddenMember = classHierarchy.getOverriddenMember(node);
if (!overriddenMember) {
return;
}

// Compute basic types (might contain type parameters)
const ownMemberType = typeComputer.computeType(node);
const overriddenMemberType = typeComputer.computeType(overriddenMember);
let overriddenMemberType = typeComputer.computeType(overriddenMember);

// Substitute type parameters on overriddenMemberType
const classContainingOwnMember = getContainerOfType(node, isSdsClass);
const typeContainingOwnMember = typeComputer.computeType(classContainingOwnMember);

if (typeContainingOwnMember instanceof ClassType) {
const classContainingOverriddenMember = getContainerOfType(overriddenMember, isSdsClass);
const typeContainingOverriddenMember = stream(typeComputer.streamSupertypes(typeContainingOwnMember)).find(
(it) => it.declaration === classContainingOverriddenMember,
);

if (typeContainingOverriddenMember) {
overriddenMemberType = overriddenMemberType.substituteTypeParameters(
typeContainingOverriddenMember.substitutions,
);
}
}

if (!typeChecker.isAssignableTo(ownMemberType, overriddenMemberType)) {
accept(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tests.validation.inheritance.overridingMethodMustMatchOverriddenMethod

class MySuperClass {
class MySuperClass1 {
attr myInstanceAttribute: Number
static attr myStaticAttribute: Number

fun myInstanceMethod(a: Number = 0) -> r: Number
static fun myStaticMethod(a: Number = 0) -> r: Number
@Pure fun myInstanceMethod(a: Number = 0) -> r: Number
@Pure static fun myStaticMethod(a: Number = 0) -> r: Number
}

class MyClass1 sub MySuperClass {
class MyClass1 sub MySuperClass1 {
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
attr »myInstanceAttribute«: Int
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
Expand All @@ -26,30 +26,30 @@ class MyClass1 sub MySuperClass {


// $TEST$ no error r"Overriding member does not match the overridden member:.*"
fun »myInstanceMethod«(a: Any = 0) -> r: Int
@Pure fun »myInstanceMethod«(a: Any = 0) -> r: Int
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
static fun »myStaticMethod«(a: Any = 0) -> r: Int
@Pure static fun »myStaticMethod«(a: Any = 0) -> r: Int

// $TEST$ no error r"Overriding member does not match the overridden member:.*"
fun »myInstanceMethod«() -> r: Int
@Pure fun »myInstanceMethod«() -> r: Int
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
static fun »myStaticMethod«() -> r: Int
@Pure static fun »myStaticMethod«() -> r: Int

// $TEST$ no error r"Overriding member does not match the overridden member:.*"
fun »myOwnInstanceMethod«(a: Any = 0) -> r: Int
@Pure fun »myOwnInstanceMethod«(a: Any = 0) -> r: Int
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
static fun »myOwnStaticMethod«(a: Any = 0) -> r: Int
@Pure static fun »myOwnStaticMethod«(a: Any = 0) -> r: Int
}

class MyClass2 sub MySuperClass {
class MyClass2 sub MySuperClass1 {
// $TEST$ error r"Overriding member does not match the overridden member:.*"
attr »myInstanceAttribute«: Any
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
static attr »myStaticAttribute«: Any


// $TEST$ error r"Overriding member does not match the overridden member:.*"
fun »myInstanceMethod«(a: Number = 0) -> r: Any
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Any
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
static fun »myStaticMethod«(a: Number = 0) -> r: Any
@Pure static fun »myStaticMethod«(a: Number = 0) -> r: Any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests.validation.inheritance.overridingMethodMustMatchOverriddenMethod

class MySuperClass2<T> {
attr myInstanceAttribute: T
@Pure fun myInstanceMethod(a: T = 0) -> r: T
}

class MyClass3 sub MySuperClass2<Number> {
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
attr »myInstanceAttribute«: Int
// $TEST$ no error r"Overriding member does not match the overridden member:.*"
@Pure fun »myInstanceMethod«(a: Any = 0) -> r: Int
}

class MyClass4 sub MySuperClass2<Number> {
// $TEST$ error r"Overriding member does not match the overridden member:.*"
attr »myInstanceAttribute«: Any
// $TEST$ error r"Overriding member does not match the overridden member:.*"
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Any
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tests.validation.inheritance.overridingMethodShouldDifferFromOverriddenMethod

class MySuperClass {
class MySuperClass1 {
attr myInstanceAttribute: Number
static attr myStaticAttribute: Number

fun myInstanceMethod(a: Number = 0) -> r: Number
static fun myStaticMethod(a: Number = 0) -> r: Number
@Pure fun myInstanceMethod(a: Number = 0) -> r: Number
@Pure static fun myStaticMethod(a: Number = 0) -> r: Number
}

class MyClass1 sub MySuperClass {
class MyClass1 sub MySuperClass1 {
// $TEST$ info "Overriding member is identical to overridden member and can be removed."
attr »myInstanceAttribute«: Number
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
Expand All @@ -26,30 +26,30 @@ class MyClass1 sub MySuperClass {


// $TEST$ info "Overriding member is identical to overridden member and can be removed."
fun »myInstanceMethod«(a: Number = 0) -> r: Number
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Number
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
static fun »myStaticMethod«(a: Number = 0) -> r: Number
@Pure static fun »myStaticMethod«(a: Number = 0) -> r: Number

// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
fun »myInstanceMethod«() -> r: Number
@Pure fun »myInstanceMethod«() -> r: Number
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
static fun »myStaticMethod«() -> r: Number
@Pure static fun »myStaticMethod«() -> r: Number

// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
fun »myOwnInstanceMethod«(a: Number = 0) -> r: Number
@Pure fun »myOwnInstanceMethod«(a: Number = 0) -> r: Number
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
static fun »myOwnStaticMethod«(a: Number = 0) -> r: Number
@Pure static fun »myOwnStaticMethod«(a: Number = 0) -> r: Number
}

class MyClass2 sub MySuperClass {
class MyClass2 sub MySuperClass1 {
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
attr »myInstanceAttribute«: Int
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
static attr »myStaticAttribute«: Int


// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
fun »myInstanceMethod«(a: Number = 0) -> r: Int
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Int
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
static fun »myStaticMethod«(a: Number = 0) -> r: Int
@Pure static fun »myStaticMethod«(a: Number = 0) -> r: Int
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package tests.validation.inheritance.overridingMethodShouldDifferFromOverriddenMethod

class MySuperClass {
class MySuperClass2 {
@Impure([ImpurityReason.Other])
fun myInstanceMethod(a: Number = 0) -> r: Number
@Impure([ImpurityReason.Other])
static fun myStaticMethod(a: Number = 0) -> r: Number
}

class MyClass1 sub MySuperClass {
class MyClass3 sub MySuperClass2 {
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
@Pure
fun »myInstanceMethod«(a: Number = 0) -> r: Number
Expand All @@ -16,7 +16,7 @@ class MyClass1 sub MySuperClass {
static fun »myStaticMethod«(a: Number = 0) -> r: Number
}

class MyClass2 sub MySuperClass {
class MyClass4 sub MySuperClass2 {
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
@Impure([])
fun »myInstanceMethod«(a: Number = 0) -> r: Number
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tests.validation.inheritance.overridingMethodShouldDifferFromOverriddenMethod

class MySuperClass3<T> {
attr myInstanceAttribute: T
@Pure fun myInstanceMethod(a: T = 0) -> r: T
}

class MyClass5 sub MySuperClass3<Number> {
// $TEST$ info "Overriding member is identical to overridden member and can be removed."
attr »myInstanceAttribute«: Number
// $TEST$ info "Overriding member is identical to overridden member and can be removed."
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Number
}

class MyClass6 sub MySuperClass3<Number> {
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
attr »myInstanceAttribute«: Int
// $TEST$ no info "Overriding member is identical to overridden member and can be removed."
@Pure fun »myInstanceMethod«(a: Number = 0) -> r: Int
}