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.48.22~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0423a6337884a319cafdba369525e406a0e0c822;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 4b9aaa2a4..740ea5490 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -49,6 +49,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 1aafdc6b6..99136bee1 100644 --- a/tests/basic-types/strings.vala +++ b/tests/basic-types/strings.vala @@ -35,6 +35,11 @@ void test_string () { assert (t[1] == 'l'); } +void test_string_concat () { + var s = "hello" + "world"; + assert (s == "helloworld"); +} + void test_string_joinv () { string[] sa = { "hello", "my", "world" }; @@ -121,6 +126,7 @@ void test_string_substring () { void main () { test_string (); + test_string_concat (); test_string_joinv (); test_string_replace (); test_string_slice (); diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala index 20ad71d05..3f1737d7f 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;