Skip to content

Commit

Permalink
Add clearer error messages when using set/get incorrectly.
Browse files Browse the repository at this point in the history
It's quite easy for users to forget to use the colon before their
setters and getters, but in this situation error messages are unclear about
the real nature of the problem.

This commit adds a check for malformed set/get statements and provides
error messages containing more direct guidance to users.

Closes #91216
  • Loading branch information
ArchieVillain committed May 3, 2024
1 parent 02deedc commit 568874c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,34 @@ GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static, b
}

complete_extents(variable);

bool malformed_setget_found = false;

if (p_allow_property && check(GDScriptTokenizer::Token::IDENTIFIER)) {
StringName identifier_name = current.get_identifier();
if (identifier_name == SNAME("set") || identifier_name == SNAME("get")) {
push_error(vformat(R"(Expected ":" before "%s".)", identifier_name));
malformed_setget_found = true;
}
}

end_statement("variable declaration");

if (p_allow_property && !malformed_setget_found && match(GDScriptTokenizer::Token::INDENT)) {
if (check(GDScriptTokenizer::Token::IDENTIFIER)) {
StringName identifier_name = current.get_identifier();
if (identifier_name == SNAME("set") || identifier_name == SNAME("get")) {
malformed_setget_found = true;
}
}

if (malformed_setget_found) {
push_error(vformat(R"(Expected ":" at end of variable declaration when using "set" or "get")"), variable);
} else {
push_error(R"(Unexpected "Indent" after variable declaration.)");
}
}

return variable;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#GDTEST_PARSER_ERROR

var property
get():
return 0

func test():
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected ":" at end of variable declaration when using "set" or "get"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#GDTEST_PARSER_ERROR

var property get = get_property

func test():
pass

func get_property():
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected ":" before "get".

0 comments on commit 568874c

Please sign in to comment.