Godot Engine – Multi-platform 2D and 3D game engine
Change type validation error messages to be more readable
fish0eecb0858038
diff --git a/core/variant/array.cpp b/core/variant/array.cppindex 111241a11fa..f23ae619ee4 100644--- a/core/variant/array.cpp+++ b/core/variant/array.cpp@@ -235,7 +235,7 @@ void Array::assign(const Array &p_array) {for (int i = 0; i < size; i++) {const Variant &element = source[i];if (element.get_type() != Variant::NIL && (element.get_type() != Variant::OBJECT || !typed.validate_object(element, "assign"))) {- ERR_FAIL_MSG(vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(element.get_type()), Variant::get_type_name(typed.type)));+ ERR_FAIL_MSG(vformat("Unable to convert array index %d from '%s' to '%s'.", i, Variant::get_type_name(element.get_type()), Variant::get_type_name(typed.type)));}}_p->array = p_array._p->array;@@ -258,11 +258,11 @@ void Array::assign(const Array &p_array) {continue;}if (!Variant::can_convert_strict(value->get_type(), typed.type)) {- ERR_FAIL_MSG(vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));+ ERR_FAIL_MSG(vformat("Unable to convert array index %d from '%s' to '%s'.", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));}Callable::CallError ce;Variant::construct(typed.type, data[i], &value, 1, ce);- ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));+ ERR_FAIL_COND_MSG(ce.error, vformat("Unable to convert array index %d from '%s' to '%s'.", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));}} else if (Variant::can_convert_strict(source_typed.type, typed.type)) {// from primitives to different convertible primitives@@ -270,10 +270,10 @@ void Array::assign(const Array &p_array) {const Variant *value = source + i;Callable::CallError ce;Variant::construct(typed.type, data[i], &value, 1, ce);- ERR_FAIL_COND_MSG(ce.error, vformat(R"(Unable to convert array index %d from "%s" to "%s".)", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));+ ERR_FAIL_COND_MSG(ce.error, vformat("Unable to convert array index %d from '%s' to '%s'.", i, Variant::get_type_name(value->get_type()), Variant::get_type_name(typed.type)));}} else {- ERR_FAIL_MSG(vformat(R"(Cannot assign contents of "Array[%s]" to "Array[%s]".)", Variant::get_type_name(source_typed.type), Variant::get_type_name(typed.type)));+ ERR_FAIL_MSG(vformat("Cannot assign contents of 'Array[%s]' to 'Array[%s]'.", Variant::get_type_name(source_typed.type), Variant::get_type_name(typed.type)));}_p->array = array;@@ -408,7 +408,7 @@ int Array::find_custom(const Callable &p_callable, int p_from) const {ERR_FAIL_V_MSG(ret, vformat("Error calling method from 'find_custom': %s.", Variant::get_callable_error_text(p_callable, argptrs, 1, ce)));}- ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, ret, "Error on method from 'find_custom': Return type of callable must be boolean.");+ ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, ret, "Error calling method from 'find_custom': Return type of callable must be boolean.");if (res.operator bool()) {return i;}@@ -468,7 +468,7 @@ int Array::rfind_custom(const Callable &p_callable, int p_from) const {ERR_FAIL_V_MSG(-1, vformat("Error calling method from 'rfind_custom': %s.", Variant::get_callable_error_text(p_callable, argptrs, 1, ce)));}- ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, -1, "Error on method from 'rfind_custom': Return type of callable must be boolean.");+ ERR_FAIL_COND_V_MSG(res.get_type() != Variant::Type::BOOL, -1, "Error calling method from 'rfind_custom': Return type of callable must be boolean.");if (res.operator bool()) {return i;}@@ -496,7 +496,7 @@ int Array::count(const Variant &p_value) const {bool Array::has(const Variant &p_value) const {Variant value = p_value;- ERR_FAIL_COND_V(!_p->typed.validate(value, "use 'has'"), false);+ ERR_FAIL_COND_V(!_p->typed.validate(value, "use 'has' with"), false);return find(value) != -1;}@@ -755,13 +755,13 @@ void Array::shuffle() {int Array::bsearch(const Variant &p_value, bool p_before) const {Variant value = p_value;- ERR_FAIL_COND_V(!_p->typed.validate(value, "binary search"), -1);+ ERR_FAIL_COND_V(!_p->typed.validate(value, "binary search using"), -1);return _p->array.span().bisect<_ArrayVariantSort>(value, p_before);}int Array::bsearch_custom(const Variant &p_value, const Callable &p_callable, bool p_before) const {Variant value = p_value;- ERR_FAIL_COND_V(!_p->typed.validate(value, "custom binary search"), -1);+ ERR_FAIL_COND_V(!_p->typed.validate(value, "binary search with a custom comparator using"), -1);return _p->array.bsearch_custom<CallableComparator>(value, p_before, p_callable);}diff --git a/core/variant/container_type_validate.h b/core/variant/container_type_validate.hindex 9d9f7702ad9..ac2d4cc6369 100644--- a/core/variant/container_type_validate.h+++ b/core/variant/container_type_validate.h@@ -70,7 +70,7 @@ private:}if (p_output_errors) {- ERR_FAIL_V_MSG(false, vformat("Attempted to %s a variable of type '%s' into a %s of type '%s'.", String(p_operation), Variant::get_type_name(inout_variant.get_type()), where, Variant::get_type_name(type)));+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s a variable of type '%s' into a %s of incompatible type '%s'.", String(p_operation), Variant::get_type_name(inout_variant.get_type()), where, Variant::get_type_name(type)));} else {return false;}@@ -112,7 +112,13 @@ private:const StringName &obj_class = object->get_class_name();if (obj_class != class_name && !object->is_class(class_name)) {if (p_output_errors) {- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object of type '%s' into a %s, which does not inherit from '%s'.", String(p_operation), obj_class, where, String(class_name)));+ String object_class_name = object->get_class();+ if (const Ref<Script> other_script = object->get_script(); other_script.is_valid()) {+ if (const StringName &script_global_name = other_script->get_global_name(); !script_global_name.is_empty()) {+ object_class_name = script_global_name;+ }+ }+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object of type '%s' into a %s of incompatible type '%s'.", String(p_operation), object_class_name, where, String(class_name)));} else {return false;}@@ -127,14 +133,14 @@ private:// Check base script..if (other_script.is_null()) {if (p_output_errors) {- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s, that does not inherit from '%s'.", String(p_operation), String(where), String(script->get_class_name())));+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));} else {return false;}}if (!other_script->inherits_script(script)) {if (p_output_errors) {- ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s, that does not inherit from '%s'.", String(p_operation), String(where), String(script->get_class_name())));+ ERR_FAIL_V_MSG(false, vformat("Attempted to %s an object into a %s of incompatible type '%s'.", String(p_operation), String(where), String(script->get_class_name())));} else {return false;}diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_array.out b/modules/gdscript/tests/scripts/runtime/errors/typed_array.outindex 28a000a63eb..ab46089122e 100644--- a/modules/gdscript/tests/scripts/runtime/errors/typed_array.out+++ b/modules/gdscript/tests/scripts/runtime/errors/typed_array.out@@ -3,7 +3,7 @@ GDTEST_RUNTIME_ERROR>> SCRIPT ERROR at runtime/errors/typed_array.gd:10 on subtest_assign_basic_to_typed(): Trying to assign an array of type "Array" to a variable of type "Array[int]".>> SCRIPT ERROR at runtime/errors/typed_array.gd:15 on subtest_assign_basic_to_differently_typed(): Trying to assign an array of type "Array[float]" to a variable of type "Array[int]".>> ERROR: Method/function failed. Returning: false->> Attempted to set an object into a TypedArray, that does not inherit from 'GDScript'.+>> Attempted to set an object into a TypedArray of incompatible type 'GDScript'.>> ERROR: Condition "!_p->typed.validate(value, "set")" is true.end subtest_assign_wrong_to_typed>> SCRIPT ERROR at runtime/errors/typed_array.gd:24 on subtest_pass_basic_to_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_array.gd)'. The array of argument 1 (Array) does not have the same element type as the expected typed array argument.diff --git a/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out b/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.outindex 4110f7fe541..b390c32df78 100644--- a/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out+++ b/modules/gdscript/tests/scripts/runtime/errors/typed_dictionary.out@@ -2,16 +2,16 @@ GDTEST_RUNTIME_ERROR~~ WARNING at line 45: (UNSAFE_CALL_ARGUMENT) The argument 1 of the function "expect_typed()" requires the subtype "Dictionary[int, int]" but the supertype "Variant" was provided.>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:16 on subtest_assign_basic_to_typed(): Trying to assign a dictionary of type "Dictionary" to a variable of type "Dictionary[int, int]".>> ERROR: Method/function failed. Returning: false->> Attempted to set a variable of type 'String' into a TypedDictionary.Key of type 'int'.+>> Attempted to set a variable of type 'String' into a TypedDictionary.Key of incompatible type 'int'.>> ERROR: Condition "!_p->typed_key.validate(key, "set")" is true. Returning: false>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:21 on subtest_assign_basic_to_differently_typed_key(): Invalid assignment of property or key 'key' with value of type 'int' on a base object of type 'Dictionary[int, int]'.>> ERROR: Method/function failed. Returning: false->> Attempted to set a variable of type 'String' into a TypedDictionary.Value of type 'int'.+>> Attempted to set a variable of type 'String' into a TypedDictionary.Value of incompatible type 'int'.>> ERROR: Condition "!_p->typed_value.validate(value, "set")" is true. Returning: false>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:26 on subtest_assign_basic_to_differently_typed_value(): Invalid assignment of property or key '0' with value of type 'String' on a base object of type 'Dictionary[int, int]'.>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:31 on subtest_assign_differently_typed(): Trying to assign a dictionary of type "Dictionary[float, float]" to a variable of type "Dictionary[int, int]".>> ERROR: Method/function failed. Returning: false->> Attempted to set an object into a TypedDictionary.Key, that does not inherit from 'GDScript'.+>> Attempted to set an object into a TypedDictionary.Key of incompatible type 'GDScript'.>> ERROR: Condition "!_p->typed_key.validate(key, "set")" is true. Returning: falseend subtest_assign_wrong_to_typed>> SCRIPT ERROR at runtime/errors/typed_dictionary.gd:40 on subtest_pass_basic_to_typed(): Invalid type in function 'expect_typed' in base 'RefCounted (typed_dictionary.gd)'. The dictionary of argument 1 (Dictionary) does not have the same element type as the expected typed dictionary argument.