From 0c1584455101907ff5cce151c43879be2e3bf7a7 Mon Sep 17 00:00:00 2001 From: George Marques Date: Thu, 29 Dec 2022 08:57:41 -0300 Subject: GDScript: Error when assigning return value of void function This also makes built-in method calls empty the return value when the method is void, to avoid keeping returning a garbage value in such case. --- .../tests/scripts/runtime/errors/use_return_value_of_free_call.gd | 4 ++++ .../scripts/runtime/errors/use_return_value_of_free_call.out | 6 ++++++ .../errors/use_return_value_of_void_builtin_method_call.gd | 4 ++++ .../errors/use_return_value_of_void_builtin_method_call.out | 6 ++++++ .../runtime/errors/use_return_value_of_void_native_method_call.gd | 4 ++++ .../errors/use_return_value_of_void_native_method_call.out | 6 ++++++ .../runtime/features/array_string_stringname_equivalent.gd | 8 ++++---- 7 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.gd create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.out create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.gd create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.out create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.gd create mode 100644 modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.out (limited to 'modules/gdscript/tests/scripts') diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.gd b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.gd new file mode 100644 index 0000000000..a3daf70627 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.gd @@ -0,0 +1,4 @@ +func test(): + var obj + obj = Node.new() + print(obj.free()) diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.out b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.out new file mode 100644 index 0000000000..5edaf19442 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_free_call.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/use_return_value_of_free_call.gd +>> 4 +>> Trying to get a return value of a method that returns "void" diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.gd b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.gd new file mode 100644 index 0000000000..49fb76ad1f --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.gd @@ -0,0 +1,4 @@ +func test(): + var value + value = [] + print(value.reverse()) diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.out b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.out new file mode 100644 index 0000000000..128356ff8a --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_builtin_method_call.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/use_return_value_of_void_builtin_method_call.gd +>> 4 +>> Trying to get a return value of a method that returns "void" diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.gd b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.gd new file mode 100644 index 0000000000..44f9aa467a --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.gd @@ -0,0 +1,4 @@ +func test(): + var obj + obj = RefCounted.new() + print(obj.notify_property_list_changed()) diff --git a/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.out b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.out new file mode 100644 index 0000000000..e02c206778 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/errors/use_return_value_of_void_native_method_call.out @@ -0,0 +1,6 @@ +GDTEST_RUNTIME_ERROR +>> SCRIPT ERROR +>> on function: test() +>> runtime/errors/use_return_value_of_void_native_method_call.gd +>> 4 +>> Trying to get a return value of a method that returns "void" diff --git a/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd index 5303fb04e2..9b64084fa6 100644 --- a/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd +++ b/modules/gdscript/tests/scripts/runtime/features/array_string_stringname_equivalent.gd @@ -15,10 +15,10 @@ func test(): var string_array: Array[String] = [] var stringname_array: Array[StringName] = [] - assert(!string_array.push_back(&"abc")) + string_array.push_back(&"abc") print("Array[String] insert converted: ", typeof(string_array[0]) == TYPE_STRING) - assert(!stringname_array.push_back("abc")) + stringname_array.push_back("abc") print("Array[StringName] insert converted: ", typeof(stringname_array[0]) == TYPE_STRING_NAME) print("StringName in Array[String]: ", &"abc" in string_array) @@ -28,8 +28,8 @@ func test(): assert(!packed_string_array.push_back("abc")) print("StringName in PackedStringArray: ", &"abc" in packed_string_array) - assert(!string_array.push_back("abc")) + string_array.push_back("abc") print("StringName finds String in Array: ", string_array.find(&"abc")) - assert(!stringname_array.push_back(&"abc")) + stringname_array.push_back(&"abc") print("String finds StringName in Array: ", stringname_array.find("abc")) -- cgit v1.2.3 From bc739a46878d06bd77fcb924a8bd079071057be0 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 30 Dec 2022 11:57:25 -0300 Subject: GDScript: Make using return of void function an error Remove the `VOID_ASSIGNMENT` warning since those cases will be errors now. --- .../analyzer/errors/use_value_of_void_function_builtin_method.gd | 3 +++ .../analyzer/errors/use_value_of_void_function_builtin_method.out | 2 ++ .../analyzer/errors/use_value_of_void_function_custom_method.gd | 5 +++++ .../analyzer/errors/use_value_of_void_function_custom_method.out | 2 ++ .../analyzer/errors/use_value_of_void_function_gd_utility.gd | 2 ++ .../analyzer/errors/use_value_of_void_function_gd_utility.out | 2 ++ .../analyzer/errors/use_value_of_void_function_native_method.gd | 3 +++ .../analyzer/errors/use_value_of_void_function_native_method.out | 2 ++ .../scripts/analyzer/errors/use_value_of_void_function_utility.gd | 2 ++ .../scripts/analyzer/errors/use_value_of_void_function_utility.out | 2 ++ modules/gdscript/tests/scripts/parser/warnings/void_assignment.gd | 6 ------ modules/gdscript/tests/scripts/parser/warnings/void_assignment.out | 5 ----- 12 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.out create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.out create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.out create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.out create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.out delete mode 100644 modules/gdscript/tests/scripts/parser/warnings/void_assignment.gd delete mode 100644 modules/gdscript/tests/scripts/parser/warnings/void_assignment.out (limited to 'modules/gdscript/tests/scripts') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.gd b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.gd new file mode 100644 index 0000000000..a3450966cc --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.gd @@ -0,0 +1,3 @@ +func test(): + var builtin := [] + print(builtin.reverse()) # Built-in type method. diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.out b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.out new file mode 100644 index 0000000000..225c85e9c7 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_builtin_method.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot get return value of call to "reverse()" because it returns "void". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.gd b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.gd new file mode 100644 index 0000000000..2162a181ac --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.gd @@ -0,0 +1,5 @@ +func foo() -> void: + pass + +func test(): + print(foo()) # Custom method. diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.out b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.out new file mode 100644 index 0000000000..2b1a607883 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_custom_method.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot get return value of call to "foo()" because it returns "void". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.gd b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.gd new file mode 100644 index 0000000000..f3443d985e --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.gd @@ -0,0 +1,2 @@ +func test(): + print(print_debug()) # GDScript utility function. diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.out b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.out new file mode 100644 index 0000000000..502c18ab9d --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_gd_utility.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot get return value of call to "print_debug()" because it returns "void". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.gd b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.gd new file mode 100644 index 0000000000..b8e81b160a --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.gd @@ -0,0 +1,3 @@ +func test(): + var obj := Node.new() + print(obj.free()) # Native type method. diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.out b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.out new file mode 100644 index 0000000000..88be39345b --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_native_method.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot get return value of call to "free()" because it returns "void". diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.gd b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.gd new file mode 100644 index 0000000000..8eabed4271 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.gd @@ -0,0 +1,2 @@ +func test(): + print(print()) # Built-in utility function. diff --git a/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.out b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.out new file mode 100644 index 0000000000..ebf43186be --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/use_value_of_void_function_utility.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot get return value of call to "print()" because it returns "void". diff --git a/modules/gdscript/tests/scripts/parser/warnings/void_assignment.gd b/modules/gdscript/tests/scripts/parser/warnings/void_assignment.gd deleted file mode 100644 index b4a42b3e3d..0000000000 --- a/modules/gdscript/tests/scripts/parser/warnings/void_assignment.gd +++ /dev/null @@ -1,6 +0,0 @@ -func i_return_void() -> void: - return - - -func test(): - var __ = i_return_void() diff --git a/modules/gdscript/tests/scripts/parser/warnings/void_assignment.out b/modules/gdscript/tests/scripts/parser/warnings/void_assignment.out deleted file mode 100644 index 84c9598f9a..0000000000 --- a/modules/gdscript/tests/scripts/parser/warnings/void_assignment.out +++ /dev/null @@ -1,5 +0,0 @@ -GDTEST_OK ->> WARNING ->> Line: 6 ->> VOID_ASSIGNMENT ->> Assignment operation, but the function 'i_return_void()' returns void. -- cgit v1.2.3