1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-10 13:00:37 +00:00

Merge pull request #69991 from rune-scape/cast-type

GDScript: Fix cast producing null
This commit is contained in:
Rémi Verschelde
2022-12-23 09:22:21 +01:00
3 changed files with 25 additions and 12 deletions

View File

@@ -490,9 +490,11 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
} break; } break;
case GDScriptParser::Node::CAST: { case GDScriptParser::Node::CAST: {
const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression); 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); GDScriptDataType cast_type = _gdtype_from_datatype(og_cast_type, codegen.script);
GDScriptCodeGenerator::Address result;
if (cast_type.has_type) {
if (og_cast_type.kind == GDScriptParser::DataType::ENUM) { 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. // 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.kind = GDScriptDataType::BUILTIN;
@@ -500,7 +502,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
} }
// Create temporary for result first since it will be deleted last. // Create temporary for result first since it will be deleted last.
GDScriptCodeGenerator::Address result = codegen.add_temporary(cast_type); 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);
@@ -509,6 +511,9 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) { if (src.mode == GDScriptCodeGenerator::Address::TEMPORARY) {
gen->pop_temporary(); gen->pop_temporary();
} }
} else {
result = _parse_expression(codegen, r_error, cn->operand);
}
return result; return result;
} break; } break;

View File

@@ -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)

View File

@@ -0,0 +1,3 @@
GDTEST_OK
cast to Variant == null: false
cast to Object == null: false