From: wxx <769218589@qq.com> Date: Sat, 4 Dec 2021 09:17:47 +0000 (+0800) Subject: vala: NullLiteral is not a valid argument for string concatenation X-Git-Tag: 0.52.10~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=db1ab2cf16e77f5fd9d5c878b4c0f755f4d4f8b7;p=thirdparty%2Fvala.git vala: NullLiteral is not a valid argument for string concatenation Fixes https://gitlab.gnome.org/GNOME/vala/issues/1260 --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 484e0d5be..c097fbc20 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -50,6 +50,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/tests/basic-types/strings.vala b/tests/basic-types/strings.vala index e1cfd9afb..22c96b7f9 100644 --- a/tests/basic-types/strings.vala +++ b/tests/basic-types/strings.vala @@ -48,6 +48,11 @@ void test_string () { assert (t == s); } +void test_string_concat () { + var s = "hello" + "world"; + assert (s == "helloworld"); +} + void test_string_joinv () { string[] sa = { "hello", "my", "world" }; @@ -139,6 +144,7 @@ void test_string_substring () { void main () { test_string (); + test_string_concat (); test_string_joinv (); test_string_printf (); test_string_replace (); diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala index d379927c0..6545e1cff 100644 --- a/vala/valabinaryexpression.vala +++ b/vala/valabinaryexpression.vala @@ -359,7 +359,9 @@ public class Vala.BinaryExpression : Expression { && left.value_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;