Fix dictionary variable matching dictionary

This commit is contained in:
Richard Menzies 2024-09-18 23:05:24 +01:00
parent b93e188919
commit b50c88b590

View file

@ -2561,6 +2561,50 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc
}
if (p_match_pattern->pattern_type == GDScriptParser::PatternNode::PT_DICTIONARY) {
if (p_match_test->type == GDScriptParser::Node::Type::IDENTIFIER) {
Pair<GDScriptParser::DataType, GDScriptParser::DataType> other_type_found;
bool incorrect_type_present = false;
bool incorrect_key_present = false;
bool has_value = false;
GDScriptParser::DataType correct_type_key;
GDScriptParser::DataType correct_type_value;
if (p_match_test->get_datatype().container_element_types.size() == 2) {
// Both dict key and value specified
correct_type_key = p_match_test->get_datatype().container_element_types[0];
correct_type_value = p_match_test->get_datatype().container_element_types[1];
has_value = true;
} else {
break; // dictionary has no type
}
for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
// Don't count the ".." symbol
if (p_match_pattern->dictionary[i].value_pattern && p_match_pattern->dictionary[i].value_pattern->pattern_type == GDScriptParser::PatternNode::PT_REST) {
continue;
}
if (p_match_pattern->dictionary[i].value_pattern) {
// Test key and value type, key and value given
if (p_match_pattern->dictionary[i].key->datatype != correct_type_key || (has_value && p_match_pattern->dictionary[i].value_pattern->datatype != correct_type_value)) {
incorrect_type_present = true;
other_type_found = Pair(p_match_pattern->dictionary[i].key->datatype, p_match_pattern->dictionary[i].value_pattern->datatype);
break;
}
} else {
// Only test key type, value is not given
if (p_match_pattern->dictionary[i].key->datatype != correct_type_key) {
incorrect_key_present = true;
other_type_found = Pair(p_match_pattern->dictionary[i].key->datatype, GDScriptParser::DataType());
}
}
}
if (incorrect_type_present || incorrect_key_present) {
parser->push_warning(p_match_pattern, GDScriptWarning::MISMATCHED_TYPE, Variant::get_type_name(Variant::Type::DICTIONARY), p_match_test->get_datatype().to_string(), "dict_typed", "[" + other_type_found.first.to_string() + (incorrect_key_present ? "" : ", " + other_type_found.second.to_string()) + "]", "");
}
break;
} else if (p_match_test->type == GDScriptParser::Node::Type::DICTIONARY) {
GDScriptParser::DictionaryNode *dict_node_match = dynamic_cast<GDScriptParser::DictionaryNode *>(p_match_test);
bool open_ended = false;
@ -2657,6 +2701,7 @@ void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_matc
parser->push_warning(p_match_pattern, GDScriptWarning::MISMATCHED_TYPE, Variant::get_type_name(Variant::Type::DICTIONARY), p_match_test->get_datatype().to_string(), "dict", missing_values, "");
}
}
}
break;
case Variant::ARRAY:
if (p_match_pattern->get_datatype() != p_match_test->get_datatype()) {