From 030995c10da93cbc0c3a3f00c73ee07948cbe543 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 12 Apr 2024 11:49:21 -0300 Subject: [PATCH] GDScript: Allow casting enum to int An enum value is always an integer so the cast is valid. The code here now consider this case to avoid giving an error message. --- modules/gdscript/gdscript_analyzer.cpp | 2 ++ .../tests/scripts/analyzer/features/cast_enum_to_int.gd | 9 +++++++++ .../tests/scripts/analyzer/features/cast_enum_to_int.out | 2 ++ 3 files changed, 13 insertions(+) create mode 100644 modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.out diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index a30a87f3a1c1..b198338ff087 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -3469,6 +3469,8 @@ void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) { if (op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) { mark_node_unsafe(p_cast); valid = true; + } else if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.builtin_type == Variant::INT) { + valid = true; } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) { valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type); } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) { diff --git a/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.gd b/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.gd new file mode 100644 index 000000000000..77ef9e2073ba --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.gd @@ -0,0 +1,9 @@ +# GH-85882 + +enum Foo { A, B, C } + +func test(): + var a := Foo.A + var b := a as int + 1 + print(b) + diff --git a/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.out b/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.out new file mode 100644 index 000000000000..a7f1357bb23c --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/cast_enum_to_int.out @@ -0,0 +1,2 @@ +GDTEST_OK +1