]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Use DataType.compatible() to check for string concatenation
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 1 Nov 2020 20:16:49 +0000 (21:16 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 3 Jan 2021 12:04:03 +0000 (13:04 +0100)
Make the checks match the ones performed by the code-generator to prevent
invalid c-code to be created.

See https://gitlab.gnome.org/GNOME/vala/issues/1100

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

index 18560b63bd36666cdad89d92d322a52c1310bc63..9d428d5ff529b3d16689a874bfe3b383035ac819 100644 (file)
@@ -32,6 +32,7 @@ TESTS = \
        basic-types/arrays-fixed-assignment.vala \
        basic-types/array-uint8-uchar-compat.vala \
        basic-types/pointers.vala \
+       basic-types/pointers-arithmetic.vala \
        basic-types/sizeof.vala \
        basic-types/garray.vala \
        basic-types/glists.vala \
@@ -879,6 +880,7 @@ NON_NULL_TESTS = \
        nullability/member-access-nullable-instance.test \
        nullability/method-parameter-invalid-convert.test \
        nullability/method-return-invalid-convert.test \
+       nullability/string-concat.test \
        $(NULL)
 
 LINUX_TESTS = \
diff --git a/tests/basic-types/pointers-arithmetic.vala b/tests/basic-types/pointers-arithmetic.vala
new file mode 100644 (file)
index 0000000..79437f0
--- /dev/null
@@ -0,0 +1,22 @@
+void test_chars () {
+       char* s = "foo";
+       char* begin = s;
+       char* end = begin + 2;
+
+       assert (begin[0] == 'f');
+       assert (end[0] == 'o');
+}
+
+void test_strings () {
+       string s = "foo";
+       string* begin = s;
+       string* end = begin + s.length - 1;
+
+       assert (((char*) begin)[0] == 'f');
+       assert (((char*) end)[0] == 'o');
+}
+
+void main () {
+       test_chars ();
+       test_strings ();
+}
diff --git a/tests/nullability/string-concat.test b/tests/nullability/string-concat.test
new file mode 100644 (file)
index 0000000..5cc78ef
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       string? foo = null;
+       string bar = foo + "bar";
+}
index 783cdc05558e7fd57911d667e286b49b1e599c57..421ebf1658910cf19892afbd863e8b5f2bed4768 100644 (file)
@@ -343,11 +343,11 @@ public class Vala.BinaryExpression : Expression {
                right.target_type = right.value_type.copy ();
                right.target_type.value_owned = false;
 
-               if (left.value_type.data_type == context.analyzer.string_type.data_type
-                   && operator == BinaryOperator.PLUS) {
+               if (operator == BinaryOperator.PLUS && !(left.value_type is PointerType)
+                   && left.value_type.compatible (context.analyzer.string_type)) {
                        // string concatenation
 
-                       if (right.value_type == null || right.value_type.data_type != context.analyzer.string_type.data_type) {
+                       if (right.value_type == null || !right.value_type.compatible (context.analyzer.string_type)) {
                                error = true;
                                Report.error (source_reference, "Operands must be strings");
                                return false;