]> 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 09:11:44 +0000 (10:11 +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 4b9aaa2a457b901d2e7c78acc1f7ef2083834bdb..740ea5490c1aaacacfc5a5854465876ee1244761 100644 (file)
@@ -49,6 +49,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 1aafdc6b638818c2f4ceef275e13574340bfb2a3..99136bee1f81205262d64e247d9cbe0950e7d320 100644 (file)
@@ -35,6 +35,11 @@ void test_string () {
        assert (t[1] == 'l');
 }
 
+void test_string_concat () {
+       var s = "hello" + "world";
+       assert (s == "helloworld");
+}
+
 void test_string_joinv () {
        string[] sa = { "hello", "my", "world" };
 
@@ -121,6 +126,7 @@ void test_string_substring () {
 
 void main () {
        test_string ();
+       test_string_concat ();
        test_string_joinv ();
        test_string_replace ();
        test_string_slice ();
index 20ad71d05478eedf01c049206e8ef7439bee651c..3f1737d7fd4bbfa1dcf90fa1562909382c2357d4 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;