]> 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>
Mon, 2 Nov 2020 10:00:51 +0000 (11:00 +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 168a3fe6ebb095dac139312931fcd594844e679f..57020feeea89b279c3ef72a1b3bd328127c7fd69 100644 (file)
@@ -35,6 +35,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 \
@@ -1026,6 +1027,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 \
        nullability/with-non-null.test \
        $(NULL)
 
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 7b9b7821472c301045172db6c2ce9021c536b9c9..f831c7cb7b2b61df73ab95584234b4e0c32f5c5d 100644 (file)
@@ -350,11 +350,11 @@ public class Vala.BinaryExpression : Expression {
                right.target_type = right.value_type.copy ();
                right.target_type.value_owned = false;
 
-               if (operator == BinaryOperator.PLUS
-                   && left.value_type.type_symbol == context.analyzer.string_type.type_symbol) {
+               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.type_symbol != context.analyzer.string_type.type_symbol) {
+                       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;