diff options
author | wxx <769218589@qq.com> | 2021-12-04 17:17:47 +0800 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2022-01-09 10:11:44 +0100 |
commit | 0423a6337884a319cafdba369525e406a0e0c822 (patch) | |
tree | 1a574b7f3bce37a0dde0db219856da3e14c7a1a0 | |
parent | 0d8d6545967002ba1cd2ed13324ec25c702b4e3e (diff) | |
download | vala-0423a6337884a319cafdba369525e406a0e0c822.tar.gz |
vala: NullLiteral is not a valid argument for string concatenation
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1260
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/basic-types/string-concat-null.test | 5 | ||||
-rw-r--r-- | tests/basic-types/strings.vala | 6 | ||||
-rw-r--r-- | vala/valabinaryexpression.vala | 4 |
4 files changed, 15 insertions, 1 deletions
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; |