Skip to content

Commit

Permalink
Merge pull request #48139 from vnen/gdscript-dict-keys
Browse files Browse the repository at this point in the history
Fix mismatch between String and StringName in dictionary keys
  • Loading branch information
akien-mga authored Apr 24, 2021
2 parents db90ab8 + 1e4ff2e commit d1dc28e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
24 changes: 4 additions & 20 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2621,25 +2621,6 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri

GDScriptParser::DataType result_type;

// Reduce index first. If it's a constant StringName, use attribute instead.
if (!p_subscript->is_attribute) {
if (p_subscript->index == nullptr) {
return;
}
reduce_expression(p_subscript->index);

if (p_subscript->index->is_constant && p_subscript->index->reduced_value.get_type() == Variant::STRING_NAME) {
GDScriptParser::IdentifierNode *attribute = parser->alloc_node<GDScriptParser::IdentifierNode>();
// Copy location for better error message.
attribute->start_line = p_subscript->index->start_line;
attribute->end_line = p_subscript->index->end_line;
attribute->leftmost_column = p_subscript->index->leftmost_column;
attribute->rightmost_column = p_subscript->index->rightmost_column;
p_subscript->is_attribute = true;
p_subscript->attribute = attribute;
}
}

if (p_subscript->is_attribute) {
if (p_subscript->attribute == nullptr) {
return;
Expand Down Expand Up @@ -2682,7 +2663,10 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri
}
}
} else {
// Index was already reduced before.
if (p_subscript->index == nullptr) {
return;
}
reduce_expression(p_subscript->index);

if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
// Just try to get it.
Expand Down
8 changes: 4 additions & 4 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
}
break;
case GDScriptParser::DictionaryNode::LUA_TABLE:
// Lua-style: key is an identifier interpreted as string.
String key = static_cast<const GDScriptParser::IdentifierNode *>(dn->elements[i].key)->name;
// Lua-style: key is an identifier interpreted as StringName.
StringName key = static_cast<const GDScriptParser::IdentifierNode *>(dn->elements[i].key)->name;
element = codegen.add_constant(key);
break;
}
Expand Down Expand Up @@ -680,9 +680,9 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
name = subscript->attribute->name;
named = true;
} else {
if (subscript->index->type == GDScriptParser::Node::LITERAL && static_cast<const GDScriptParser::LiteralNode *>(subscript->index)->value.get_type() == Variant::STRING) {
if (subscript->index->is_constant && subscript->index->reduced_value.get_type() == Variant::STRING_NAME) {
// Also, somehow, named (speed up anyway).
name = static_cast<const GDScriptParser::LiteralNode *>(subscript->index)->value;
name = subscript->index->reduced_value;
named = true;
} else {
// Regular indexing.
Expand Down
2 changes: 2 additions & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,8 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_dictionary(ExpressionNode
push_error(R"(Expected "=" after dictionary key.)");
}
}
key->is_constant = true;
key->reduced_value = static_cast<IdentifierNode *>(key)->name;
break;
case DictionaryNode::PYTHON_DICT:
if (!match(GDScriptTokenizer::Token::COLON)) {
Expand Down

0 comments on commit d1dc28e

Please sign in to comment.