GDScript: Check if method signature matches the parent

To guarantee polymorphism, a method signature must be compatible with
the parent. This checks if:

1. Return type is the same.
2. The subclass method takes at least the same amount of parameters.
3. The matching parameters have the same type.
4. If the subclass takes more parameters, all of the extra ones have a
default value.
5. If the superclass has default values, so must have the subclass.

There's a few test cases to ensure this holds up.
This commit is contained in:
George Marques 2022-03-06 11:09:12 -03:00
parent 272b355954
commit 1ebcb58e69
No known key found for this signature in database
GPG key ID: 046BD46A3201E43D
14 changed files with 141 additions and 9 deletions

View file

@ -0,0 +1,10 @@
func test():
print("Shouldn't reach this")
class Parent:
func my_function(_par1: int) -> int:
return 0
class Child extends Parent:
func my_function() -> int:
return 0

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "int my_function(int)".

View file

@ -0,0 +1,10 @@
func test():
print("Shouldn't reach this")
class Parent:
func my_function(_par1: int) -> int:
return 0
class Child extends Parent:
func my_function(_pary1: int, _par2: int) -> int:
return 0

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "int my_function(int)".

View file

@ -0,0 +1,10 @@
func test():
print("Shouldn't reach this")
class Parent:
func my_function(_par1: int = 0) -> int:
return 0
class Child extends Parent:
func my_function(_par1: int) -> int:
return 0

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "int my_function(int = default)".

View file

@ -0,0 +1,10 @@
func test():
print("Shouldn't reach this")
class Parent:
func my_function(_par1: int) -> int:
return 0
class Child extends Parent:
func my_function(_par1: Vector2) -> int:
return 0

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "int my_function(int)".

View file

@ -0,0 +1,10 @@
func test():
print("Shouldn't reach this")
class Parent:
func my_function() -> int:
return 0
class Child extends Parent:
func my_function() -> Vector2:
return Vector2()

View file

@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "int my_function()".

View file

@ -0,0 +1,17 @@
func test():
var instance := Parent.new()
var result := instance.my_function(1)
print(result)
assert(result == 1)
instance = Child.new()
result = instance.my_function(2)
print(result)
assert(result == 0)
class Parent:
func my_function(par1: int) -> int:
return par1
class Child extends Parent:
func my_function(_par1: int, par2: int = 0) -> int:
return par2