[vala/0.54] vala: NullLiteral is not a valid argument for string concatenation



commit 5633cf5f907b2ded6a5a3cc0259a9b5b3d92e24e
Author: wxx <769218589 qq com>
Date:   Sat Dec 4 17:17:47 2021 +0800

    vala: NullLiteral is not a valid argument for string concatenation
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1260

 tests/Makefile.am                         |  1 +
 tests/basic-types/string-concat-null.test |  5 +++++
 tests/basic-types/strings.c-expected      | 13 +++++++++++++
 tests/basic-types/strings.vala            |  6 ++++++
 vala/valabinaryexpression.vala            |  4 +++-
 5 files changed, 28 insertions(+), 1 deletion(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9b3caa8ae..4dfbfbfff 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -51,6 +51,7 @@ TESTS = \
        basic-types/custom-types.vala \
        basic-types/default-gtype.vala \
        basic-types/strings.vala \
+       basic-types/string-concat-null.test \
        basic-types/arrays.vala \
        basic-types/arrays-generics.vala \
        basic-types/arrays-fixed-assignment.vala \
diff --git a/tests/basic-types/string-concat-null.test b/tests/basic-types/string-concat-null.test
new file mode 100644
index 000000000..f12632d8b
--- /dev/null
+++ b/tests/basic-types/string-concat-null.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       var str = "foo" + null + "bar";
+}
diff --git a/tests/basic-types/strings.c-expected b/tests/basic-types/strings.c-expected
index 0a18eb8fa..69f444356 100644
--- a/tests/basic-types/strings.c-expected
+++ b/tests/basic-types/strings.c-expected
@@ -23,6 +23,7 @@
 #define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
 
 VALA_EXTERN void test_string (void);
+VALA_EXTERN void test_string_concat (void);
 VALA_EXTERN void test_string_joinv (void);
 VALA_EXTERN void test_string_printf (void);
 VALA_EXTERN void test_string_replace (void);
@@ -240,6 +241,17 @@ test_string (void)
        _g_free0 (s);
 }
 
+void
+test_string_concat (void)
+{
+       gchar* s = NULL;
+       gchar* _tmp0_;
+       _tmp0_ = g_strdup ("hello" "world");
+       s = _tmp0_;
+       _vala_assert (g_strcmp0 (s, "helloworld") == 0, "s == \"helloworld\"");
+       _g_free0 (s);
+}
+
 static gchar*
 _vala_g_strjoinv (const gchar* separator,
                   gchar** str_array,
@@ -853,6 +865,7 @@ static void
 _vala_main (void)
 {
        test_string ();
+       test_string_concat ();
        test_string_joinv ();
        test_string_printf ();
        test_string_replace ();
diff --git a/tests/basic-types/strings.vala b/tests/basic-types/strings.vala
index e1cfd9afb..22c96b7f9 100644
--- a/tests/basic-types/strings.vala
+++ b/tests/basic-types/strings.vala
@@ -48,6 +48,11 @@ void test_string () {
        assert (t == s);
 }
 
+void test_string_concat () {
+       var s = "hello" + "world";
+       assert (s == "helloworld");
+}
+
 void test_string_joinv () {
        string[] sa = { "hello", "my", "world" };
 
@@ -139,6 +144,7 @@ void test_string_substring () {
 
 void main () {
        test_string ();
+       test_string_concat ();
        test_string_joinv ();
        test_string_printf ();
        test_string_replace ();
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index d379927c0..6545e1cff 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -359,7 +359,9 @@ public class Vala.BinaryExpression : Expression {
                    && left.value_type.compatible (context.analyzer.string_type)) {
                        // string concatenation
 
-                       if (right.value_type == null || !right.value_type.compatible 
(context.analyzer.string_type)) {
+                       if (right.value_type == null || !right.value_type.compatible 
(context.analyzer.string_type)
+                           || left is NullLiteral || right is NullLiteral) {
+                               // operands cannot be null
                                error = true;
                                Report.error (source_reference, "Operands must be strings");
                                return false;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]