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 d937f54
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,36 @@ GDScriptParser::VariableNode *GDScriptParser::parse_variable(bool p_is_static, b
}

complete_extents(variable);

bool malformed_setget_found = false;

if (p_allow_property && match(GDScriptTokenizer::Token::IDENTIFIER)) {
StringName identifier_name = previous.get_identifier();
if (identifier_name == SNAME("set") || identifier_name == SNAME("get")) {
push_error(vformat(R"(Expected ":" before "%s".)", identifier_name));
malformed_setget_found = true;
} else {
push_error(R"(Expected end of statement after variable declaration, found "Identifier" instead.)");
}
}

end_statement("variable declaration");

if (p_allow_property && !malformed_setget_found && match(GDScriptTokenizer::Token::INDENT)) {
if (match(GDScriptTokenizer::Token::IDENTIFIER)) {
StringName identifier_name = previous.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

0 comments on commit d937f54

Please sign in to comment.