GDScript: Add static typing for for loop variable

This commit is contained in:
Danil Alexeev
2023-08-04 12:19:11 +03:00
parent 0511f9d9a7
commit 6c59ed9485
20 changed files with 177 additions and 17 deletions

View File

@@ -0,0 +1,4 @@
func test():
var a: Array[Resource] = []
for node: Node in a:
print(node)

View File

@@ -0,0 +1,2 @@
GDTEST_ANALYZER_ERROR
Unable to iterate on value of type "Array[Resource]" with variable of type "Node".

View File

@@ -0,0 +1,4 @@
func test():
var a: Array[Node] = []
for node: Node in a:
print(node)

View File

@@ -0,0 +1,5 @@
GDTEST_OK
>> WARNING
>> Line: 3
>> REDUNDANT_FOR_VARIABLE_TYPE
>> The for loop iterator "node" already has inferred type "Node", the specified type is redundant.

View File

@@ -0,0 +1,4 @@
func test():
var a: Array[Node2D] = []
for node: Node in a:
print(node)

View File

@@ -0,0 +1,5 @@
GDTEST_OK
>> WARNING
>> Line: 3
>> REDUNDANT_FOR_VARIABLE_TYPE
>> The for loop iterator "node" has inferred type "Node2D" but its supertype "Node" is specified.

View File

@@ -0,0 +1,4 @@
func test():
var a: Array = [Resource.new()]
for node: Node in a:
print(node)

View File

@@ -0,0 +1,6 @@
GDTEST_RUNTIME_ERROR
>> SCRIPT ERROR
>> on function: test()
>> runtime/errors/for_loop_iterator_type_not_match_specified.gd
>> 3
>> Trying to assign value of type 'Resource' to a variable of type 'Node'.

View File

@@ -0,0 +1,34 @@
func test():
print("Test range.")
for e: float in range(2, 5):
var elem := e
prints(var_to_str(e), var_to_str(elem))
print("Test int.")
for e: float in 3:
var elem := e
prints(var_to_str(e), var_to_str(elem))
print("Test untyped int array.")
var a1 := [10, 20, 30]
for e: float in a1:
var elem := e
prints(var_to_str(e), var_to_str(elem))
print("Test typed int array.")
var a2: Array[int] = [10, 20, 30]
for e: float in a2:
var elem := e
prints(var_to_str(e), var_to_str(elem))
print("Test String-keys dictionary.")
var d1 := {a = 1, b = 2, c = 3}
for k: StringName in d1:
var key := k
prints(var_to_str(k), var_to_str(key))
print("Test RefCounted-keys dictionary.")
var d2 := {RefCounted.new(): 1, Resource.new(): 2, ConfigFile.new(): 3}
for k: RefCounted in d2:
var key := k
prints(k.get_class(), key.get_class())

View File

@@ -0,0 +1,25 @@
GDTEST_OK
Test range.
2.0 2.0
3.0 3.0
4.0 4.0
Test int.
0.0 0.0
1.0 1.0
2.0 2.0
Test untyped int array.
10.0 10.0
20.0 20.0
30.0 30.0
Test typed int array.
10.0 10.0
20.0 20.0
30.0 30.0
Test String-keys dictionary.
&"a" &"a"
&"b" &"b"
&"c" &"c"
Test RefCounted-keys dictionary.
RefCounted RefCounted
Resource Resource
ConfigFile ConfigFile