diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2023-01-06 08:06:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-06 08:06:36 +0100 |
commit | 2e116271100cfb5ff4a76423b7a78891378295bd (patch) | |
tree | b91bed552852192dded3c9826e928659662b77f8 | |
parent | 95ce236b7d6a70a06ecc13fb08d48da90ed98430 (diff) | |
parent | 10e364bf43a5129220f8091e0bdd00e521ba89ea (diff) | |
download | redot-engine-2e116271100cfb5ff4a76423b7a78891378295bd.tar.gz |
Merge pull request #70980 from vonagam/fix-array-as-default-parameter
GDScript: Fix array as default value for parameter
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 |