From 1a810ff45ebda061c2f41249eca1c8df9b5991e8 Mon Sep 17 00:00:00 2001 From: Dmitrii Maganov Date: Wed, 1 Feb 2023 14:52:18 +0200 Subject: [PATCH] GDScript: Fix type certainty for result of ternary operator --- modules/gdscript/gdscript_analyzer.cpp | 2 +- .../scripts/analyzer/errors/ternary_weak_infer.gd | 6 ++++++ .../scripts/analyzer/errors/ternary_weak_infer.out | 2 ++ .../scripts/analyzer/features/ternary_hard_infer.gd | 12 ++++++++++++ .../scripts/analyzer/features/ternary_hard_infer.out | 2 ++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.out create mode 100644 modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.gd create mode 100644 modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.out diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp index 1c2b7439090..77fc1a209d2 100644 --- a/modules/gdscript/gdscript_analyzer.cpp +++ b/modules/gdscript/gdscript_analyzer.cpp @@ -3988,7 +3988,6 @@ void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternar if (!is_type_compatible(true_type, false_type)) { result = false_type; if (!is_type_compatible(false_type, true_type)) { - result.type_source = GDScriptParser::DataType::UNDETECTED; result.kind = GDScriptParser::DataType::VARIANT; #ifdef DEBUG_ENABLED parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY); @@ -3996,6 +3995,7 @@ void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternar } } } + result.type_source = true_type.is_hard_type() && false_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED; p_ternary_op->set_datatype(result); } diff --git a/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.gd b/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.gd new file mode 100644 index 00000000000..fac0e8756c2 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.gd @@ -0,0 +1,6 @@ +func test(): + var left_hard_int := 1 + var right_weak_int = 2 + var result_hm_int := left_hard_int if true else right_weak_int + + print('not ok') diff --git a/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.out b/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.out new file mode 100644 index 00000000000..71d1e2f8ae8 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/errors/ternary_weak_infer.out @@ -0,0 +1,2 @@ +GDTEST_ANALYZER_ERROR +Cannot infer the type of "result_hm_int" variable because the value doesn't have a set type. diff --git a/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.gd b/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.gd new file mode 100644 index 00000000000..fbb75306150 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.gd @@ -0,0 +1,12 @@ +func test(): + var left_hard_int := 1 + var right_hard_int := 2 + var result_hard_int := left_hard_int if true else right_hard_int + assert(result_hard_int == 1) + + var left_hard_variant := 1 as Variant + var right_hard_variant := 2.0 as Variant + var result_hard_variant := left_hard_variant if true else right_hard_variant + assert(result_hard_variant == 1) + + print('ok') diff --git a/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.out b/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.out new file mode 100644 index 00000000000..1b47ed10dc0 --- /dev/null +++ b/modules/gdscript/tests/scripts/analyzer/features/ternary_hard_infer.out @@ -0,0 +1,2 @@ +GDTEST_OK +ok