[vala/staging] vala: NullLiteral is not a valid argument for string concatenation



commit b0310278841968fca3dcef7fc2d18a5796898919
Author: wxx <769218589 qq com>
Date:   Sat Dec 4 17:17:47 2021 +0800

    vala: NullLiteral is not a valid argument for string concatenation
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1260

 tests/Makefile.am                         | 1 +
 tests/basic-types/string-concat-null.test | 5 +++++
 vala/valabinaryexpression.vala            | 7 +++++--
 3 files changed, 11 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 329fb8795..cae6103ab 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -51,6 +51,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
index 000000000..f12632d8b
--- /dev/null
+++ b/tests/basic-types/string-concat-null.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       var str = "foo" + null + "bar";
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index d379927c0..a43f890e8 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -356,10 +356,13 @@ public class Vala.BinaryExpression : Expression {
                right.target_type.value_owned = false;
 
                if (operator == BinaryOperator.PLUS && !(left.value_type is PointerType)
-                   && left.value_type.compatible (context.analyzer.string_type)) {
+                   && left.value_type.compatible (context.analyzer.string_type)
+                   && (target_type == null || target_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;


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