diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-21 23:36:06 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-21 23:36:06 +0100 |
commit | d4bec5855e8a2987f83a8bb491a5675287d460c3 (patch) | |
tree | 6d1b5625869817725b78fae88bda87ded98c4827 | |
parent | 9b774ef350a589cb20d11159a992ed276c0e70c7 (diff) | |
parent | 9ddf482a0659ee5544a8ab39ddd39e66a8920939 (diff) |
Merge pull request #73705 from anvilfolk/doublewoopsie
Added check for null objects in gdscript typed assign.
3 files changed, 45 insertions, 18 deletions
diff --git a/modules/gdscript/gdscript_vm.cpp b/modules/gdscript/gdscript_vm.cpp index ba400b8e15..83d2ed6010 100644 --- a/modules/gdscript/gdscript_vm.cpp +++ b/modules/gdscript/gdscript_vm.cpp @@ -1326,28 +1326,30 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a OPCODE_BREAK; } - ScriptInstance *scr_inst = val_obj->get_script_instance(); - if (!scr_inst) { - err_text = "Trying to assign value of type '" + val_obj->get_class_name() + - "' to a variable of type '" + base_type->get_path().get_file() + "'."; - OPCODE_BREAK; - } + if (val_obj) { // src is not null + ScriptInstance *scr_inst = val_obj->get_script_instance(); + if (!scr_inst) { + err_text = "Trying to assign value of type '" + val_obj->get_class_name() + + "' to a variable of type '" + base_type->get_path().get_file() + "'."; + OPCODE_BREAK; + } - Script *src_type = val_obj->get_script_instance()->get_script().ptr(); - bool valid = false; + Script *src_type = scr_inst->get_script().ptr(); + bool valid = false; - while (src_type) { - if (src_type == base_type) { - valid = true; - break; + while (src_type) { + if (src_type == base_type) { + valid = true; + break; + } + src_type = src_type->get_base_script().ptr(); } - src_type = src_type->get_base_script().ptr(); - } - if (!valid) { - err_text = "Trying to assign value of type '" + val_obj->get_script_instance()->get_script()->get_path().get_file() + - "' to a variable of type '" + base_type->get_path().get_file() + "'."; - OPCODE_BREAK; + if (!valid) { + err_text = "Trying to assign value of type '" + val_obj->get_script_instance()->get_script()->get_path().get_file() + + "' to a variable of type '" + base_type->get_path().get_file() + "'."; + OPCODE_BREAK; + } } } #endif // DEBUG_ENABLED diff --git a/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.gd b/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.gd new file mode 100644 index 0000000000..1b47680a7b --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.gd @@ -0,0 +1,18 @@ +extends Node + +class LocalClass extends Node: + pass + +func test(): + var typed: LocalClass = get_node_or_null("does_not_exist") + var untyped = null + var node_1: LocalClass = typed + var node_2: LocalClass = untyped + var node_3 = typed + var node_4 = untyped + print(typed) + print(untyped) + print(node_1) + print(node_2) + print(node_3) + print(node_4) diff --git a/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.out b/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.out new file mode 100644 index 0000000000..d66b72f5c3 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/script_typed_assign_null.out @@ -0,0 +1,7 @@ +GDTEST_OK +<Object#null> +<null> +<Object#null> +<null> +<Object#null> +<null> |