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

GDScript: Add missing member type check when resolving extends #75879

Merged
merged 1 commit into from
Apr 14, 2023
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
22 changes: 21 additions & 1 deletion modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,24 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
}
if (look_class->has_member(name)) {
resolve_class_member(look_class, name, id);
base = look_class->get_member(name).get_datatype();
GDScriptParser::ClassNode::Member member = look_class->get_member(name);
GDScriptParser::DataType member_datatype = member.get_datatype();

switch (member.type) {
case GDScriptParser::ClassNode::Member::CLASS:
break; // OK.
case GDScriptParser::ClassNode::Member::CONSTANT:
if (member_datatype.kind != GDScriptParser::DataType::SCRIPT && member_datatype.kind != GDScriptParser::DataType::CLASS) {
push_error(vformat(R"(Constant "%s" is not a preloaded script or class.)", name), id);
return ERR_PARSE_ERROR;
}
break;
default:
push_error(vformat(R"(Cannot use %s "%s" in extends chain.)", member.get_type_name(), name), id);
return ERR_PARSE_ERROR;
}

base = member_datatype;
found = true;
break;
}
Expand All @@ -506,6 +523,9 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
if (!id_type.is_set()) {
push_error(vformat(R"(Could not find nested type "%s".)", id->name), id);
return ERR_PARSE_ERROR;
} else if (id_type.kind != GDScriptParser::DataType::SCRIPT && id_type.kind != GDScriptParser::DataType::CLASS) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have some doubts about this. We should probably check if the identifier is a class or a constant containing a script/class. Also, I am not happy with the variety of error messages:

  • Constant "A" is not a preloaded script or class.
  • Identifier "X" is not a preloaded script or class.
  • Cannot get nested types for extension from non-GDScript type "RefCounted".
  • Could not find nested type "Baz".
  • Cannot use variable "A" in extends chain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's fix the crash for now, and iterate on this in a follow-up.

push_error(vformat(R"(Identifier "%s" is not a preloaded script or class.)", id->name), id);
return ERR_PARSE_ERROR;
}

base = id_type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# GH-75870

const A = 1

class B extends A:
pass

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Constant "A" is not a preloaded script or class.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GH-75870

class A:
const X = 1

const Y = A.X # A.X is now resolved.

class B extends A.X:
pass

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Identifier "X" is not a preloaded script or class.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# GH-75870

var A = 1

class B extends A:
pass

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Cannot use variable "A" in extends chain.