Skip to content

Commit

Permalink
Merge pull request #69991 from rune-scape/cast-type
Browse files Browse the repository at this point in the history
GDScript: Fix cast producing null
  • Loading branch information
akien-mga committed Dec 23, 2022
2 parents b31bf1f + 7d0d6aa commit ae4c025
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
29 changes: 17 additions & 12 deletions modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,24 +490,29 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
} break;
case GDScriptParser::Node::CAST: {
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
GDScriptParser::DataType og_cast_type = cn->cast_type->get_datatype();
GDScriptParser::DataType og_cast_type = cn->get_datatype();
GDScriptDataType cast_type = _gdtype_from_datatype(og_cast_type, codegen.script);

if (og_cast_type.kind == GDScriptParser::DataType::ENUM) {
// Enum types are usually treated as dictionaries, but in this case we want to cast to an integer.
cast_type.kind = GDScriptDataType::BUILTIN;
cast_type.builtin_type = Variant::INT;
}
GDScriptCodeGenerator::Address result;
if (cast_type.has_type) {
if (og_cast_type.kind == GDScriptParser::DataType::ENUM) {
// Enum types are usually treated as dictionaries, but in this case we want to cast to an integer.
cast_type.kind = GDScriptDataType::BUILTIN;
cast_type.builtin_type = Variant::INT;
}

// Create temporary for result first since it will be deleted last.
GDScriptCodeGenerator::Address result = codegen.add_temporary(cast_type);
// Create temporary for result first since it will be deleted last.
result = codegen.add_temporary(cast_type);

GDScriptCodeGenerator::Address src = _parse_expression(codegen, r_error, cn->operand);
GDScriptCodeGenerator::Address src = _parse_expression(codegen, r_error, cn->operand);

gen->write_cast(result, src, cast_type);
gen->write_cast(result, src, cast_type);

if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
gen->pop_temporary();
if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
gen->pop_temporary();
}
} else {
result = _parse_expression(codegen, r_error, cn->operand);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# https://github.com/godotengine/godot/issues/69504#issuecomment-1345725988

func test():
print("cast to Variant == null: ", 1 as Variant == null)
print("cast to Object == null: ", self as Object == null)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
cast to Variant == null: false
cast to Object == null: false

0 comments on commit ae4c025

Please sign in to comment.