Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Forbid scripts from inheriting their own inner classes
HolonProduction7f9b7257982b
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cppindex 68bb83eae05..d792c9c3820 100644--- a/modules/gdscript/gdscript_analyzer.cpp+++ b/modules/gdscript/gdscript_analyzer.cpp@@ -533,6 +533,8 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_cbool found = false;List<GDScriptParser::ClassNode *> script_classes;get_class_node_current_scope_classes(p_class, &script_classes, id);+ // Classes can't inherit from their own members or themselves.+ script_classes.erase(p_class);for (GDScriptParser::ClassNode *look_class : script_classes) {if (look_class->identifier && look_class->identifier->name == name) {if (!look_class->self_type.is_set()) {@@ -564,6 +566,15 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_creturn ERR_PARSE_ERROR;}+ if (member_datatype.class_type == p_class) {+ if (p_class->extends.size() == 1) {+ push_error("Cyclic inheritance.", id);+ } else {+ push_error("Classes can not inherit from their own members.", id);+ }+ return ERR_PARSE_ERROR;+ }+base = member_datatype;found = true;break;@@ -594,6 +605,13 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c} else if (id_type.kind != GDScriptParser::DataType::SCRIPT && id_type.kind != GDScriptParser::DataType::CLASS) {push_error(vformat(R"(Identifier "%s" is not a preloaded script or class.)", id->name), id);return ERR_PARSE_ERROR;+ } else if (id_type.class_type == p_class) {+ if (index == p_class->extends.size() - 1) {+ push_error("Cyclic inheritance.", id);+ } else {+ push_error("Classes can not inherit from their own members.", id);+ }+ return ERR_PARSE_ERROR;}base = id_type;diff --git a/modules/gdscript/tests/scripts/analyzer/errors/inner_base.gd b/modules/gdscript/tests/scripts/analyzer/errors/inner_base.gdnew file mode 100644index 00000000000..4861311fcc5--- /dev/null+++ b/modules/gdscript/tests/scripts/analyzer/errors/inner_base.gd@@ -0,0 +1,39 @@+class_name InnerBase++class A extends InnerA:+ class InnerA:+ pass++class B extends InnerB:+ const InnerB = A++class C extends InnerC.InnerInnerC:+ class InnerC:+ class InnerInnerC:+ pass++class D extends InnerBase.D:+ pass++class E extends InnerBase.E.InnerE:+ class InnerE:+ pass++class F extends F:+ pass++class G extends G.InnerG:+ class InnerG:+ pass++class H extends InnerBase:+ pass++const _I = I+class I extends _I:+ pass++const _J = J+class J extends _J.InnerJ:+ class InnerJ:+ passdiff --git a/modules/gdscript/tests/scripts/analyzer/errors/inner_base.out b/modules/gdscript/tests/scripts/analyzer/errors/inner_base.outnew file mode 100644index 00000000000..8234c6a331f--- /dev/null+++ b/modules/gdscript/tests/scripts/analyzer/errors/inner_base.out@@ -0,0 +1,10 @@+GDTEST_ANALYZER_ERROR+>> ERROR at line 3: Could not find base class "InnerA".+>> ERROR at line 7: Could not find base class "InnerB".+>> ERROR at line 10: Could not find base class "InnerC".+>> ERROR at line 15: Cyclic inheritance.+>> ERROR at line 18: Classes can not inherit from their own members.+>> ERROR at line 22: Cyclic inheritance.+>> ERROR at line 25: Classes can not inherit from their own members.+>> ERROR at line 33: Cyclic inheritance.+>> ERROR at line 37: Classes can not inherit from their own members.diff --git a/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.gd b/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.gddeleted file mode 100644index 3f9bfe189cf..00000000000--- a/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.gd+++ /dev/null@@ -1,4 +0,0 @@-extends "inner_base.gd".InnerA.InnerAB--func test():- super.test()diff --git a/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.out b/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.outdeleted file mode 100644index 62f1383392e..00000000000--- a/modules/gdscript/tests/scripts/analyzer/features/external_inner_base.out+++ /dev/null@@ -1,3 +0,0 @@-GDTEST_OK-InnerA.InnerAB.test-InnerB.testdiff --git a/modules/gdscript/tests/scripts/analyzer/features/inner_base.gd b/modules/gdscript/tests/scripts/analyzer/features/inner_base.gddeleted file mode 100644index a825b59255d..00000000000--- a/modules/gdscript/tests/scripts/analyzer/features/inner_base.gd+++ /dev/null@@ -1,18 +0,0 @@-extends InnerA--func test():- super.test()--class InnerA extends InnerAB:- func test():- print("InnerA.test")- super.test()-- class InnerAB extends InnerB:- func test():- print("InnerA.InnerAB.test")- super.test()--class InnerB:- func test():- print("InnerB.test")diff --git a/modules/gdscript/tests/scripts/analyzer/features/inner_base.out b/modules/gdscript/tests/scripts/analyzer/features/inner_base.outdeleted file mode 100644index ddd5ffcfd31..00000000000--- a/modules/gdscript/tests/scripts/analyzer/features/inner_base.out+++ /dev/null@@ -1,4 +0,0 @@-GDTEST_OK-InnerA.test-InnerA.InnerAB.test-InnerB.testdiff --git a/modules/gdscript/tests/scripts/analyzer/features/out_of_order.gd b/modules/gdscript/tests/scripts/analyzer/features/out_of_order.gdindex 11349cc9165..a2c1503d06c 100644--- a/modules/gdscript/tests/scripts/analyzer/features/out_of_order.gd+++ b/modules/gdscript/tests/scripts/analyzer/features/out_of_order.gd@@ -20,9 +20,9 @@ class InnerA extends InnerAB:func fn(p2 := E1.V2) -> String:return "%s, p2=%s" % [super.fn(), p2]- class InnerAB:- func fn(p1 := c1) -> String:- return "p1=%s" % p1+class InnerAB:+ func fn(p1 := c1) -> String:+ return "p1=%s" % p1var v2 := f()