From 6c59ed9485bbfadee73a08dfc57224e022626e6e Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Fri, 4 Aug 2023 12:19:11 +0300 Subject: GDScript: Add static typing for `for` loop variable --- .../errors/for_loop_wrong_specified_type.gd | 4 +++ .../errors/for_loop_wrong_specified_type.out | 2 ++ ...for_loop_specified_type_is_equal_to_inferred.gd | 4 +++ ...or_loop_specified_type_is_equal_to_inferred.out | 5 ++++ ...loop_specified_type_is_supertype_of_inferred.gd | 4 +++ ...oop_specified_type_is_supertype_of_inferred.out | 5 ++++ .../for_loop_iterator_type_not_match_specified.gd | 4 +++ .../for_loop_iterator_type_not_match_specified.out | 6 ++++ .../features/for_loop_iterator_specified_types.gd | 34 ++++++++++++++++++++++ .../features/for_loop_iterator_specified_types.out | 25 ++++++++++++++++ 10 files changed, 93 insertions(+) create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.out create mode 100644 modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.out create mode 100644 modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.out create mode 100644 modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.gd create mode 100644 modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.out create mode 100644 modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.out (limited to 'modules/gdscript/tests/scripts') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.gd b/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.gd new file mode 100644 index 0000000000..7e3b6e3c39 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.gd @@ -0,0 +1,4 @@ +func test(): + var a: Array[Resource] = [] + for node: Node in a: + print(node) diff --git a/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.out b/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.out new file mode 100644 index 0000000000..8f8a368f9a --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/for_loop_wrong_specified_type.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Unable to iterate on value of type "Array[Resource]" with variable of type "Node". diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.gd b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.gd new file mode 100644 index 0000000000..1b32491e48 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.gd @@ -0,0 +1,4 @@ +func test(): + var a: Array[Node] = [] + for node: Node in a: + print(node) diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.out b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.out new file mode 100644 index 0000000000..3b3fbd9bd1 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_equal_to_inferred.out @@ -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. diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.gd b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.gd new file mode 100644 index 0000000000..fcbc13c53d --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.gd @@ -0,0 +1,4 @@ +func test(): + var a: Array[Node2D] = [] + for node: Node in a: + print(node) diff --git a/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.out b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.out new file mode 100644 index 0000000000..36d4a161d3 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/warnings/for_loop_specified_type_is_supertype_of_inferred.out @@ -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. diff --git a/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.gd b/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.gd new file mode 100644 index 0000000000..cdc278ecf5 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.gd @@ -0,0 +1,4 @@ +func test(): + var a: Array = [Resource.new()] + for node: Node in a: + print(node) diff --git a/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.out b/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.out new file mode 100644 index 0000000000..b7ef07afb2 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/for_loop_iterator_type_not_match_specified.out @@ -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'. diff --git a/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.gd b/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.gd new file mode 100644 index 0000000000..58b4df5a79 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.gd @@ -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()) diff --git a/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.out b/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.out new file mode 100644 index 0000000000..f67f7d89d5 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/for_loop_iterator_specified_types.out @@ -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 -- cgit v1.2.3