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



commit f8d676b2530310e0cb10f097008d8613fa426597
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                    | 1 +
 tests/nullability/string-concat.test | 6 ++++++
 vala/valabinaryexpression.vala       | 4 ++--
 3 files changed, 9 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 168a3fe6e..561373753 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1026,6 +1026,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/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 7b9b78214..a3323290b 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -351,10 +351,10 @@ public class Vala.BinaryExpression : Expression {
                right.target_type.value_owned = false;
 
                if (operator == BinaryOperator.PLUS
-                   && left.value_type.type_symbol == context.analyzer.string_type.type_symbol) {
+                   && 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]