Godot Engine – Multi-platform 2D and 3D game engine
GDScript: Fix reflection for `_init()` and `_static_init()`
Danil Alexeev250215de6a26
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cppindex f8787405d76..49d10151042 100644--- a/modules/gdscript/gdscript_analyzer.cpp+++ b/modules/gdscript/gdscript_analyzer.cpp@@ -1839,10 +1839,14 @@ void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *}} else if (!p_is_lambda && function_name == GDScriptLanguage::get_singleton()->strings._static_init) {// Static constructor.+GDScriptParser::DataType return_type;return_type.kind = GDScriptParser::DataType::BUILTIN;return_type.builtin_type = Variant::NIL;+ return_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;+p_function->return_type_constraint = return_type;+if (p_function->return_type) {GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cppindex 99dc71174ba..2ba30c06a9e 100644--- a/modules/gdscript/gdscript_compiler.cpp+++ b/modules/gdscript/gdscript_compiler.cpp@@ -2514,7 +2514,19 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_if (p_func) {gd_function->return_type = _gdtype_from_datatype(p_func->return_type_constraint, p_script);- method_info.return_val = p_func->return_type_constraint.to_property_info(String());+ if (func_name == GDScriptLanguage::get_singleton()->strings._init) {+ // TODO: The analyzer and editor use the current class instead of `void` as the return type for `_init()`.+ // We should clear up the confusion between `new()` and `_init()` to remove the special handling here.++ GDScriptParser::DataType void_type;+ void_type.kind = GDScriptParser::DataType::BUILTIN;+ void_type.builtin_type = Variant::NIL;+ void_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;++ method_info.return_val = void_type.to_property_info(String());+ } else {+ method_info.return_val = p_func->return_type_constraint.to_property_info(String());+ }if (p_func->is_vararg()) {gd_function->_vararg_index = vararg_addr.address;diff --git a/modules/gdscript/tests/scripts/runtime/features/member_info.gd b/modules/gdscript/tests/scripts/runtime/features/member_info.gdindex 8ab3b1f908e..bf08cd5e56c 100644--- a/modules/gdscript/tests/scripts/runtime/features/member_info.gd+++ b/modules/gdscript/tests/scripts/runtime/features/member_info.gd@@ -1,5 +1,17 @@class_name TestMemberInfo+class TestSpecialBase:+ static func _static_init(): pass+ func _init(_param: int = 1): pass++class TestSpecialExplicitReturn extends TestSpecialBase:+ static func _static_init() -> void: pass+ func _init(_param: int = 2) -> void: pass++class TestSpecialImplicitReturn extends TestSpecialBase:+ static func _static_init(): pass+ func _init(_param: int = 3): pass+class MyClass:pass@@ -71,7 +83,19 @@ signal test_signal_7(a: TestMemberInfo, b: Array[TestMemberInfo], c: Dictionary[signal test_signal_8(a: MyClass, b: Array[MyClass], c: Dictionary[MyClass, MyClass])@warning_ignore_restore("unused_signal")+func check_special_methods(instance: Object) -> void:+ for method in instance.get_method_list():+ if str(method.name) in ["_init", "_static_init"]:+ var return_type: Dictionary = method.return+ print('%s # %s' % [Utils.get_method_signature(method), Utils.get_type_extended_info(return_type)])+func test():+ check_special_methods(TestSpecialExplicitReturn.new())+ print("---")+ check_special_methods(TestSpecialImplicitReturn.new())++ print("===")+var script: Script = get_script()for property in script.get_property_list():if str(property.name).begins_with("test_"):diff --git a/modules/gdscript/tests/scripts/runtime/features/member_info.out b/modules/gdscript/tests/scripts/runtime/features/member_info.outindex 952ffc85c64..6f3c1a45e9d 100644--- a/modules/gdscript/tests/scripts/runtime/features/member_info.out+++ b/modules/gdscript/tests/scripts/runtime/features/member_info.out@@ -1,4 +1,16 @@GDTEST_OK+func _init() -> void # hint=NONE hint_string="" usage=DEFAULT class_name=&""+static func _static_init() -> void # hint=NONE hint_string="" usage=NONE class_name=&""+func _init(_param: int = 2) -> void # hint=NONE hint_string="" usage=NONE class_name=&""+static func _static_init() -> void # hint=NONE hint_string="" usage=NONE class_name=&""+func _init(_param: int = 1) -> void # hint=NONE hint_string="" usage=NONE class_name=&""+---+func _init() -> void # hint=NONE hint_string="" usage=DEFAULT class_name=&""+static func _static_init() -> void # hint=NONE hint_string="" usage=NONE class_name=&""+func _init(_param: int = 3) -> void # hint=NONE hint_string="" usage=NONE class_name=&""+static func _static_init() -> void # hint=NONE hint_string="" usage=NONE class_name=&""+func _init(_param: int = 1) -> void # hint=NONE hint_string="" usage=NONE class_name=&""+===static var test_static_var_untyped: Variantstatic var test_static_var_weak_null: Variantstatic var test_static_var_weak_int: Variantdiff --git a/modules/gdscript/tests/scripts/utils.notest.gd b/modules/gdscript/tests/scripts/utils.notest.gdindex 6ae89177f9b..3e87b6316d5 100644--- a/modules/gdscript/tests/scripts/utils.notest.gd+++ b/modules/gdscript/tests/scripts/utils.notest.gd@@ -51,6 +51,15 @@ static func get_type(property: Dictionary, is_return: bool = false) -> String:return type_string(property.type)+static func get_type_extended_info(property: Dictionary) -> String:+ return 'hint=%s hint_string="%s" usage=%s class_name=&"%s"' % [+ get_property_hint_name(property.hint).trim_prefix("PROPERTY_HINT_"),+ get_property_hint_string(property).c_escape(),+ get_property_usage_string(property.usage).replace("PROPERTY_USAGE_", ""),+ str(property.class_name).c_escape(),+ ]++static func get_property_signature(property: Dictionary,base: Object = null,@@ -79,12 +88,9 @@ static func get_property_extended_info(base: Object = null,is_static: bool = false,) -> String:- return '%s\n hint=%s hint_string="%s" usage=%s class_name=&"%s"' % [+ return '%s\n %s' % [get_property_signature(property, base, is_static),- get_property_hint_name(property.hint).trim_prefix("PROPERTY_HINT_"),- get_property_hint_string(property).c_escape(),- get_property_usage_string(property.usage).replace("PROPERTY_USAGE_", ""),- str(property.class_name).c_escape(),+ get_type_extended_info(property),]