]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: NullLiteral is not a valid argument for string concatenation
authorwxx <769218589@qq.com>
Sat, 4 Dec 2021 09:17:47 +0000 (17:17 +0800)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 9 Jan 2022 08:55:01 +0000 (09:55 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1260

tests/Makefile.am
tests/basic-types/string-concat-null.test [new file with mode: 0644]
tests/basic-types/strings.vala
vala/valabinaryexpression.vala

index 484e0d5be37301014ba24e89d1178112c3901dc7..c097fbc20a77cd62ba311fff73478857fd614b8d 100644 (file)
@@ -50,6 +50,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 (file)
index 0000000..f12632d
--- /dev/null
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       var str = "foo" + null + "bar";
+}
index e1cfd9afb82161582248f7a0973520040b8f83ff..22c96b7f9208e9509c14561a6b5a12bbdc6d5005 100644 (file)
@@ -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 ();
index d379927c0abbad078f3376b95152a51c56d01024..6545e1cff1e86fbc6fcde21345ea429bbf789a28 100644 (file)
@@ -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;