summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/gdscript/gdscript_compiler.cpp13
-rw-r--r--modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.gd17
-rw-r--r--modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out8
3 files changed, 37 insertions, 1 deletions
diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp
index db32ef22b0..2d36692c3c 100644
--- a/modules/gdscript/gdscript_compiler.cpp
+++ b/modules/gdscript/gdscript_compiler.cpp
@@ -2025,7 +2025,7 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
uint32_t par_addr = codegen.generator->add_parameter(parameter->identifier->name, parameter->initializer != nullptr, par_type);
codegen.parameters[parameter->identifier->name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::FUNCTION_PARAMETER, par_addr, par_type);
- if (p_func->parameters[i]->initializer != nullptr) {
+ if (parameter->initializer != nullptr) {
optional_parameters++;
}
}
@@ -2103,6 +2103,17 @@ GDScriptFunction *GDScriptCompiler::_parse_function(Error &r_error, GDScript *p_
return nullptr;
}
GDScriptCodeGenerator::Address dst_addr = codegen.parameters[parameter->identifier->name];
+
+ // For typed arrays we need to make sure this is already initialized correctly so typed assignment work.
+ GDScriptDataType par_type = dst_addr.type;
+ if (par_type.has_type && par_type.builtin_type == Variant::ARRAY) {
+ if (par_type.has_container_element_type()) {
+ codegen.generator->write_construct_typed_array(dst_addr, par_type.get_container_element_type(), Vector<GDScriptCodeGenerator::Address>());
+ } else {
+ codegen.generator->write_construct_array(dst_addr, Vector<GDScriptCodeGenerator::Address>());
+ }
+ }
+
codegen.generator->write_assign_default_parameter(dst_addr, src_addr);
if (src_addr.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
codegen.generator->pop_temporary();
diff --git a/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.gd b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.gd
new file mode 100644
index 0000000000..fc343377fc
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.gd
@@ -0,0 +1,17 @@
+func print_untyped(array = [0]) -> void:
+ print(array)
+ print(array.get_typed_builtin())
+
+func print_inferred(array := [1]) -> void:
+ print(array)
+ print(array.get_typed_builtin())
+
+func print_typed(array: Array[int] = [2]) -> void:
+ print(array)
+ print(array.get_typed_builtin())
+
+func test():
+ print_untyped()
+ print_inferred()
+ print_typed()
+ print('ok')
diff --git a/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out
new file mode 100644
index 0000000000..082e3ade19
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/features/typed_array_as_default_parameter.out
@@ -0,0 +1,8 @@
+GDTEST_OK
+[0]
+0
+[1]
+2
+[2]
+2
+ok