[vala/0.52] vala: NullLiteral is not a valid argument for string concatenation
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.52] vala: NullLiteral is not a valid argument for string concatenation
- Date: Sun, 9 Jan 2022 09:21:18 +0000 (UTC)
commit db1ab2cf16e77f5fd9d5c878b4c0f755f4d4f8b7
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 +++++
tests/basic-types/strings.vala | 6 ++++++
vala/valabinaryexpression.vala | 4 +++-
4 files changed, 15 insertions(+), 1 deletion(-)
---
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;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]