Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Check for soft function return types in various places
HolonProduction3f32c9ace912
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cppindex 76ed5cc5adf..5a560c5772a 100644--- a/modules/gdscript/gdscript_analyzer.cpp+++ b/modules/gdscript/gdscript_analyzer.cpp@@ -1475,6 +1475,7 @@ void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, coreturn_datatype = getter_function->return_type->resolved_type;return_datatype.is_meta_type = false;}+ return_datatype = return_datatype.as_hard_type();if (getter_function->parameters.size() != 0 || return_datatype.has_no_type()) {push_error(vformat(R"(Function "%s" cannot be used as getter because of its signature.)", getter_function->identifier->name), member.variable);@@ -1518,8 +1519,8 @@ void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class, co}if (member.variable->type_constraint.is_variant() && has_valid_getter && has_valid_setter) {- if (!is_type_compatible(getter_function->return_type_constraint, setter_function->parameters[0]->type_constraint, true)) {- push_error(vformat(R"(Getter with type "%s" cannot be used along with setter of type "%s".)", getter_function->return_type_constraint.to_string(), setter_function->parameters[0]->type_constraint.to_string()), member.variable);+ if (!is_type_compatible(getter_function->return_type_constraint.as_hard_type(), setter_function->parameters[0]->type_constraint, true)) {+ push_error(vformat(R"(Getter with type "%s" cannot be used along with setter of type "%s".)", getter_function->return_type_constraint.to_string_strict(), setter_function->parameters[0]->type_constraint.to_string()), member.variable);}}}diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cppindex b632e70e154..df609235d03 100644--- a/modules/gdscript/gdscript_editor.cpp+++ b/modules/gdscript/gdscript_editor.cpp@@ -831,10 +831,10 @@ static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_functioif (p_just_args) {arghint = "(";} else {- if (p_function->return_type_constraint.builtin_type == Variant::NIL) {+ if (p_function->return_type_constraint.is_hard_type() && p_function->return_type_constraint.builtin_type == Variant::NIL) {arghint = "void " + p_function->identifier->name + "(";} else {- arghint = p_function->return_type_constraint.to_string() + " " + p_function->identifier->name + "(";+ arghint = p_function->return_type_constraint.to_string_strict() + " " + p_function->identifier->name + "(";}}diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.hindex 74f59cebccf..a7890b2d602 100644--- a/modules/gdscript/gdscript_parser.h+++ b/modules/gdscript/gdscript_parser.h@@ -144,6 +144,16 @@ public:_FORCE_INLINE_ bool is_variant() const { return kind == VARIANT || kind == RESOLVING || kind == UNRESOLVED; }_FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }+ _FORCE_INLINE_ GDScriptParser::DataType as_hard_type() const {+ if (!is_set() || has_no_type() || is_hard_type()) {+ return *this;+ }+ DataType ret;+ ret.type_source = ANNOTATED_INFERRED;+ ret.kind = VARIANT;+ return ret;+ }+String to_string() const;_FORCE_INLINE_ String to_string_strict() const { return is_hard_type() ? to_string() : "Variant"; }@@ -762,7 +772,7 @@ public:String extends_path;Vector<IdentifierNode *> extends; // List for indexing: extends A.B.CDataType base_type;- // Metatype that represents this class.+ // Metatype that represents this class. Always contains a hard-type.DataType self_type;String fqcn; // Fully-qualified class name. Identifies uniquely any class in the project.diff --git a/modules/gdscript/language_server/gdscript_extend_parser.cpp b/modules/gdscript/language_server/gdscript_extend_parser.cppindex 122fca998b5..88d28f0a470 100644--- a/modules/gdscript/language_server/gdscript_extend_parser.cpp+++ b/modules/gdscript/language_server/gdscript_extend_parser.cpp@@ -811,7 +811,7 @@ Dictionary ExtendGDScriptParser::dump_function_api(const GDScriptParser::FunctioERR_FAIL_NULL_V(p_func, Dictionary());Dictionary func;func["name"] = p_func->identifier->name;- func["return_type"] = p_func->return_type_constraint.to_string();+ func["return_type"] = p_func->return_type_constraint.to_string_strict();func["rpc_config"] = p_func->rpc_config;Array parameters;for (ParameterNode *param : p_func->parameters) {diff --git a/modules/gdscript/tests/scripts/analyzer/errors/properties.gd b/modules/gdscript/tests/scripts/analyzer/errors/properties.gdindex 2b3e2802f9a..3d523f5dc37 100644--- a/modules/gdscript/tests/scripts/analyzer/errors/properties.gd+++ b/modules/gdscript/tests/scripts/analyzer/errors/properties.gd@@ -11,7 +11,7 @@ var prop_1: String:# Setter function has wrong argument type.# Getter function has wrong return type.var prop_2: String:- set = set_prop_2, get = get_prop_2+ set = set_prop_int, get = get_prop_int# Inline setter parameter uses property type.var prop_3 := 0:@@ -19,10 +19,46 @@ var prop_3 := 0:var x: String = valueprop_3 = value-func set_prop_2(value: int):+# Untyped/variant is fine at compile time.+var prop_4: String:+ set = set_prop_untyped, get = get_prop_untyped++var prop_5: String:+ set = set_prop_variant, get = get_prop_variant++# Mismatch in getter/setter type for untyped var.+var prop6:+ set = set_prop_int, get = get_prop_string++var prop7: Variant:+ set = set_prop_int, get = get_prop_string++var prop8:+ set = set_prop_untyped, get = get_prop_int++var prop9:+ set = set_prop_int, get = get_prop_variant+++func set_prop_int(value: int):+ _prop = value++func get_prop_int() -> int:+ return _prop++func get_prop_string() -> String:+ return ""++func set_prop_untyped(value):+ _prop = value++func get_prop_untyped():+ return _prop++func set_prop_variant(value: Variant):_prop = value-func get_prop_2():+func get_prop_variant() -> Variant:return _propfunc test():diff --git a/modules/gdscript/tests/scripts/analyzer/errors/properties.out b/modules/gdscript/tests/scripts/analyzer/errors/properties.outindex a51513defbf..08e81b24ad4 100644--- a/modules/gdscript/tests/scripts/analyzer/errors/properties.out+++ b/modules/gdscript/tests/scripts/analyzer/errors/properties.out@@ -4,3 +4,5 @@ GDTEST_ANALYZER_ERROR>> ERROR at line 13: Function with return type "int" cannot be used as getter for a property of type "String".>> ERROR at line 13: Function with argument type "int" cannot be used as setter for a property of type "String".>> ERROR at line 19: Cannot assign a value of type int to variable "x" with specified type String.+>> ERROR at line 30: Getter with type "String" cannot be used along with setter of type "int".+>> ERROR at line 33: Getter with type "String" cannot be used along with setter of type "int".