You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-12 13:20:55 +00:00
GDScript: Fix type for index subscript on constant
This commit is contained in:
@@ -3534,12 +3534,12 @@ void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscri
|
|||||||
Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
|
Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
|
push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
|
||||||
|
result_type.kind = GDScriptParser::DataType::VARIANT;
|
||||||
} else {
|
} else {
|
||||||
p_subscript->is_constant = true;
|
p_subscript->is_constant = true;
|
||||||
p_subscript->reduced_value = value;
|
p_subscript->reduced_value = value;
|
||||||
result_type = type_from_variant(value, p_subscript);
|
result_type = type_from_variant(value, p_subscript);
|
||||||
}
|
}
|
||||||
result_type.kind = GDScriptParser::DataType::VARIANT;
|
|
||||||
} else {
|
} else {
|
||||||
GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
|
GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
|
||||||
GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
|
GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
|
||||||
@@ -3800,8 +3800,6 @@ void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array, bool p_is_const) {
|
void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array, bool p_is_const) {
|
||||||
bool all_is_constant = true;
|
|
||||||
|
|
||||||
for (int i = 0; i < p_array->elements.size(); i++) {
|
for (int i = 0; i < p_array->elements.size(); i++) {
|
||||||
GDScriptParser::ExpressionNode *element = p_array->elements[i];
|
GDScriptParser::ExpressionNode *element = p_array->elements[i];
|
||||||
|
|
||||||
@@ -3811,8 +3809,7 @@ void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array, bool
|
|||||||
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element), p_is_const);
|
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element), p_is_const);
|
||||||
}
|
}
|
||||||
|
|
||||||
all_is_constant = all_is_constant && element->is_constant;
|
if (!element->is_constant) {
|
||||||
if (!all_is_constant) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3830,8 +3827,6 @@ void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array, bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary, bool p_is_const) {
|
void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary, bool p_is_const) {
|
||||||
bool all_is_constant = true;
|
|
||||||
|
|
||||||
for (int i = 0; i < p_dictionary->elements.size(); i++) {
|
for (int i = 0; i < p_dictionary->elements.size(); i++) {
|
||||||
const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
|
const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
|
||||||
|
|
||||||
@@ -3841,8 +3836,7 @@ void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_d
|
|||||||
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value), p_is_const);
|
const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value), p_is_const);
|
||||||
}
|
}
|
||||||
|
|
||||||
all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
|
if (!element.key->is_constant || !element.value->is_constant) {
|
||||||
if (!all_is_constant) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
const base := [0]
|
||||||
|
|
||||||
|
func test():
|
||||||
|
var sub := base[0]
|
||||||
|
if sub is String: pass
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
Expression is of type "int" so it can't be of type "String".
|
||||||
Reference in New Issue
Block a user