[vala/0.48] vala: Use DataType.compatible() to check for string concatenation



commit 220fe8054595890daad5ae373b72321c5fa99aaf
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Nov 1 21:16:49 2020 +0100

    vala: Use DataType.compatible() to check for string concatenation
    
    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                          |  2 ++
 tests/basic-types/pointers-arithmetic.vala | 22 ++++++++++++++++++++++
 tests/nullability/string-concat.test       |  6 ++++++
 vala/valabinaryexpression.vala             |  6 +++---
 4 files changed, 33 insertions(+), 3 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6086e712c..62cba0d72 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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 \
@@ -987,6 +988,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
index 000000000..79437f087
--- /dev/null
+++ b/tests/basic-types/pointers-arithmetic.vala
@@ -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
index 000000000..5cc78efc6
--- /dev/null
+++ b/tests/nullability/string-concat.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       string? foo = null;
+       string bar = foo + "bar";
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index f88ac3848..501820076 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -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;


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