Skip to content

Commit

Permalink
Don't allow weaker return type in overriden method
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Apr 15, 2024
1 parent 4728ff3 commit d2c95e6
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,8 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *
} else {
valid = valid && is_type_compatible(parent_return_type, return_type);
}
} else if (!parent_return_type.is_variant() && !parent_return_type.is_void()) {
valid = false;
}

int par_count_diff = p_function->parameters.size() - parameters_types.size();
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class GDScriptParser {
_FORCE_INLINE_ bool is_resolving() const { return kind == RESOLVING; }
_FORCE_INLINE_ bool has_no_type() const { return type_source == UNDETECTED; }
_FORCE_INLINE_ bool is_variant() const { return kind == VARIANT || kind == RESOLVING || kind == UNRESOLVED; }
_FORCE_INLINE_ bool is_void() const { return kind == BUILTIN && builtin_type == Variant::NIL; }
_FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }

String to_string() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Parent:
func override_me() -> bool:
return true

class Child extends Parent:
func override_me():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "override_me() -> bool".
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Parent:
func override_me() -> bool:
return true

class Child extends Parent:
func override_me() -> int:
return 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
The function signature doesn't match the parent. Parent signature is "override_me() -> bool".
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Parent:
func override_me() -> Variant:
return true

class Child extends Parent:
func override_me():
pass

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Parent:
func override_me() -> void:
pass

class Child extends Parent:
func override_me():
pass

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Parent:
func override_me():
pass

class Child extends Parent:
func override_me() -> bool:
return true

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GDTEST_OK

0 comments on commit d2c95e6

Please sign in to comment.