You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
GDScript: Disallow type inference with untyped initializer
This commit is contained in:
@@ -1544,10 +1544,8 @@ void GDScriptAnalyzer::resolve_assignable(GDScriptParser::AssignableNode *p_assi
|
|||||||
GDScriptParser::DataType initializer_type = p_assignable->initializer->get_datatype();
|
GDScriptParser::DataType initializer_type = p_assignable->initializer->get_datatype();
|
||||||
|
|
||||||
if (p_assignable->infer_datatype) {
|
if (p_assignable->infer_datatype) {
|
||||||
if (!initializer_type.is_set() || initializer_type.has_no_type()) {
|
if (!initializer_type.is_set() || initializer_type.has_no_type() || !initializer_type.is_hard_type()) {
|
||||||
push_error(vformat(R"(Cannot infer the type of "%s" %s because the value doesn't have a set type.)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
|
push_error(vformat(R"(Cannot infer the type of "%s" %s because the value doesn't have a set type.)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
|
||||||
} else if (initializer_type.is_variant() && !initializer_type.is_hard_type()) {
|
|
||||||
push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is Variant. Use explicit "Variant" type if this is intended.)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
|
|
||||||
} else if (initializer_type.kind == GDScriptParser::DataType::BUILTIN && initializer_type.builtin_type == Variant::NIL && !is_constant) {
|
} else if (initializer_type.kind == GDScriptParser::DataType::BUILTIN && initializer_type.builtin_type == Variant::NIL && !is_constant) {
|
||||||
push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is "null".)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
|
push_error(vformat(R"(Cannot infer the type of "%s" %s because the value is "null".)", p_assignable->identifier->name, p_kind), p_assignable->initializer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
func test():
|
||||||
|
var untyped = 1
|
||||||
|
var inferred := untyped
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
Cannot infer the type of "inferred" variable because the value doesn't have a set type.
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
var untyped = 1
|
||||||
|
var inferred := untyped
|
||||||
|
|
||||||
|
func test():
|
||||||
|
pass
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
Cannot infer the type of "inferred" variable because the value doesn't have a set type.
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
func check(untyped = 1, inferred := untyped):
|
||||||
|
pass
|
||||||
|
|
||||||
|
func test():
|
||||||
|
check()
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
GDTEST_ANALYZER_ERROR
|
||||||
|
Cannot infer the type of "inferred" parameter because the value doesn't have a set type.
|
||||||
@@ -1,9 +1,4 @@
|
|||||||
func test():
|
func test():
|
||||||
var one_0 = 0
|
|
||||||
one_0 = 1
|
|
||||||
var one_1 := one_0
|
|
||||||
print(one_1)
|
|
||||||
|
|
||||||
var two: Variant = 0
|
var two: Variant = 0
|
||||||
two += 2
|
two += 2
|
||||||
print(two)
|
print(two)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
GDTEST_OK
|
GDTEST_OK
|
||||||
1
|
|
||||||
2
|
2
|
||||||
3
|
3
|
||||||
4
|
4
|
||||||
|
|||||||
Reference in New Issue
Block a user