Bladeren bron

GDScript: Check duplicate keys in dictionaries and enums

George Marques 5 jaren geleden
bovenliggende
commit
cd3f51c67c
2 gewijzigde bestanden met toevoegingen van 17 en 1 verwijderingen
  1. 10 0
      modules/gdscript/gdscript_analyzer.cpp
  2. 7 1
      modules/gdscript/gdscript_parser.cpp

+ 10 - 0
modules/gdscript/gdscript_analyzer.cpp

@@ -1961,6 +1961,8 @@ void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
 void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
 	bool all_is_constant = true;
 
+	HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
+
 	for (int i = 0; i < p_dictionary->elements.size(); i++) {
 		const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
 		if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
@@ -1968,6 +1970,14 @@ void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dicti
 		}
 		reduce_expression(element.value);
 		all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
+
+		if (element.key->is_constant) {
+			if (elements.has(element.key->reduced_value)) {
+				push_error(vformat(R"(Key "%s" was already used in this dictionary (at line %d).)", element.key->reduced_value, elements[element.key->reduced_value]->start_line), element.key);
+			} else {
+				elements[element.key->reduced_value] = element.value;
+			}
+		}
 	}
 
 	if (all_is_constant) {

+ 7 - 1
modules/gdscript/gdscript_parser.cpp

@@ -1022,6 +1022,8 @@ GDScriptParser::EnumNode *GDScriptParser::parse_enum() {
 	push_multiline(true);
 	consume(GDScriptTokenizer::Token::BRACE_OPEN, vformat(R"(Expected "{" after %s.)", named ? "enum name" : R"("enum")"));
 
+	HashMap<StringName, int> elements;
+
 	do {
 		if (check(GDScriptTokenizer::Token::BRACE_CLOSE)) {
 			break; // Allow trailing comma.
@@ -1033,7 +1035,9 @@ GDScriptParser::EnumNode *GDScriptParser::parse_enum() {
 			item.line = previous.start_line;
 			item.leftmost_column = previous.leftmost_column;
 
-			if (!named) {
+			if (elements.has(item.identifier->name)) {
+				push_error(vformat(R"(Name "%s" was already in this enum (at line %d).)", item.identifier->name, elements[item.identifier->name]), item.identifier);
+			} else if (!named) {
 				// TODO: Abstract this recursive member check.
 				ClassNode *parent = current_class;
 				while (parent != nullptr) {
@@ -1045,6 +1049,8 @@ GDScriptParser::EnumNode *GDScriptParser::parse_enum() {
 				}
 			}
 
+			elements[item.identifier->name] = item.line;
+
 			if (match(GDScriptTokenizer::Token::EQUAL)) {
 				ExpressionNode *value = parse_expression(false);
 				if (value == nullptr) {