Skip to content

Commit b3704e6

Browse files
committed
Fixes GDScript define nested dictionary and array as constants godotengine#50285
1 parent 1d21779 commit b3704e6

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

modules/gdscript/gdscript_analyzer.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,12 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri
31593159
reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
31603160
} else {
31613161
reduce_expression(p_subscript->base);
3162+
3163+
if (p_subscript->base->type == GDScriptParser::Node::ARRAY) {
3164+
const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_subscript->base));
3165+
} else if (p_subscript->base->type == GDScriptParser::Node::DICTIONARY) {
3166+
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_subscript->base));
3167+
}
31623168
}
31633169

31643170
GDScriptParser::DataType result_type;
@@ -3476,6 +3482,13 @@ void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
34763482

34773483
for (int i = 0; i < p_array->elements.size(); i++) {
34783484
GDScriptParser::ExpressionNode *element = p_array->elements[i];
3485+
3486+
if (element->type == GDScriptParser::Node::ARRAY) {
3487+
const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element));
3488+
} else if (element->type == GDScriptParser::Node::DICTIONARY) {
3489+
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element));
3490+
}
3491+
34793492
all_is_constant = all_is_constant && element->is_constant;
34803493
if (!all_is_constant) {
34813494
return;
@@ -3496,6 +3509,13 @@ void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_d
34963509

34973510
for (int i = 0; i < p_dictionary->elements.size(); i++) {
34983511
const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
3512+
3513+
if (element.value->type == GDScriptParser::Node::ARRAY) {
3514+
const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element.value));
3515+
} else if (element.value->type == GDScriptParser::Node::DICTIONARY) {
3516+
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value));
3517+
}
3518+
34993519
all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
35003520
if (!all_is_constant) {
35013521
return;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GDTEST_ANALYZER_ERROR
2-
Invalid index type "bool" for a base of type "Array".
2+
Cannot get index "true" from "[0, 1]".
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# https://github.com/godotengine/godot/issues/50285
2+
3+
@warning_ignore(unused_local_constant)
4+
func test():
5+
const CONST_INNER_DICTIONARY = { "key": true }
6+
const CONST_NESTED_DICTIONARY_OLD_WORKAROUND = {
7+
"key1": "value1",
8+
"key2": CONST_INNER_DICTIONARY
9+
}
10+
# All of these should be valid
11+
const CONST_NESTED_DICTIONARY = {
12+
"key1": "value1",
13+
"key2": { "key": true }
14+
}
15+
16+
17+
const CONST_DICTIONARY_WITH_ARRAY = {
18+
"key1": [1,2,3,4]
19+
}
20+
21+
const CONST_NESTED_ARRAY = [[],[2],[1,2,3]]
22+
const CONST_ARRAY_WITH_DICT = [{"key1": 3}, {"key2": 5}]
23+
24+
const THREE_DIMENSIONAL_ARRAY = [[[],[],[]],[[],[],[]],[[],[],[]]]
25+
const MANY_NESTED_DICT = {
26+
"key1": {
27+
"key11": {
28+
"key111": {},
29+
"key112": {},
30+
},
31+
"key12": {
32+
"key121": {},
33+
"key122": {},
34+
},
35+
},
36+
"key2": {
37+
"key21": {
38+
"key211": {},
39+
"key212": {},
40+
},
41+
"key22": {
42+
"key221": {},
43+
"key222": {},
44+
},
45+
}
46+
}
47+
48+
49+
const CONST_ARRAY_ACCESS = [1,2,3][0]
50+
const CONST_DICT_ACCESS = {"key1": 5}["key1"]
51+
52+
const CONST_ARRAY_NESTED_ACCESS = [[1,2,3],[4,5,6],[8,9,10]][0][1]
53+
const CONST_DICT_NESTED_ACCESS = {"key1": {"key2": 1}}["key1"]["key2"]
54+
55+
print(CONST_ARRAY_ACCESS)
56+
print(CONST_DICT_ACCESS)
57+
print(CONST_ARRAY_NESTED_ACCESS)
58+
print(CONST_DICT_NESTED_ACCESS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GDTEST_OK
2+
1
3+
5
4+
2
5+
1

0 commit comments

Comments
 (0)