summaryrefslogtreecommitdiffstats
path: root/modules/gdscript
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-04-10 17:49:49 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-04-10 17:49:49 +0200
commit67076c167dfc767a8dd9bc51c7329e9544ba799e (patch)
tree32af5298a7ff7f48dd65f85ca9e30c444dd7a0fc /modules/gdscript
parent8611fd8400fcc2053baa2d001ef7b9d8705b9d8a (diff)
parent4bdba718c5fcb2bd58827b8ede3ea7d0c4251fbd (diff)
downloadredot-engine-67076c167dfc767a8dd9bc51c7329e9544ba799e.tar.gz
Merge pull request #90448 from vnen/gdscript-infer-string-format
GDScript: Infer type with string format operator
Diffstat (limited to 'modules/gdscript')
-rw-r--r--modules/gdscript/gdscript_analyzer.cpp5
-rw-r--r--modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.gd6
-rw-r--r--modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.out2
3 files changed, 13 insertions, 0 deletions
diff --git a/modules/gdscript/gdscript_analyzer.cpp b/modules/gdscript/gdscript_analyzer.cpp
index f8cfd29b9b..a30a87f3a1 100644
--- a/modules/gdscript/gdscript_analyzer.cpp
+++ b/modules/gdscript/gdscript_analyzer.cpp
@@ -2864,6 +2864,11 @@ void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_o
result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
result.kind = GDScriptParser::DataType::BUILTIN;
result.builtin_type = Variant::BOOL;
+ } else if (p_binary_op->variant_op == Variant::OP_MODULE && left_type.builtin_type == Variant::STRING) {
+ // The modulo operator (%) on string acts as formatting and will always return a string.
+ result.type_source = left_type.type_source;
+ result.kind = GDScriptParser::DataType::BUILTIN;
+ result.builtin_type = Variant::STRING;
} else if (left_type.is_variant() || right_type.is_variant()) {
// Cannot infer type because one operand can be anything.
result.kind = GDScriptParser::DataType::VARIANT;
diff --git a/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.gd b/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.gd
new file mode 100644
index 0000000000..c83a3a8a14
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.gd
@@ -0,0 +1,6 @@
+# GH-88082
+
+func test():
+ var x = 1
+ var message := "value: %s" % x
+ print(message)
diff --git a/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.out b/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.out
new file mode 100644
index 0000000000..cf6464a4c3
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/features/infer_type_on_string_format.out
@@ -0,0 +1,2 @@
+GDTEST_OK
+value: 1