summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2020-11-01 21:16:49 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2020-11-02 11:00:51 +0100
commit88178a7dbe5bbedc24d672fc3e0a2d9a3086048f (patch)
treeee6df2cec49c68e54d39649e12c0c4d6e0ed4f02
parente3319dde5b16636fc04bc28e39236e445b69d920 (diff)
downloadvala-88178a7dbe5bbedc24d672fc3e0a2d9a3086048f.tar.gz
vala: Use DataType.compatible() to check for string concatenation
Make the checks match the ones performed by the code-generator to prevent invalid c-code to be created. See https://gitlab.gnome.org/GNOME/vala/issues/1100
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/basic-types/pointers-arithmetic.vala22
-rw-r--r--tests/nullability/string-concat.test6
-rw-r--r--vala/valabinaryexpression.vala6
4 files changed, 33 insertions, 3 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 168a3fe6e..57020feee 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -35,6 +35,7 @@ TESTS = \
basic-types/arrays-fixed-assignment.vala \
basic-types/array-uint8-uchar-compat.vala \
basic-types/pointers.vala \
+ basic-types/pointers-arithmetic.vala \
basic-types/sizeof.vala \
basic-types/garray.vala \
basic-types/glists.vala \
@@ -1026,6 +1027,7 @@ NON_NULL_TESTS = \
nullability/member-access-nullable-instance.test \
nullability/method-parameter-invalid-convert.test \
nullability/method-return-invalid-convert.test \
+ nullability/string-concat.test \
nullability/with-non-null.test \
$(NULL)
diff --git a/tests/basic-types/pointers-arithmetic.vala b/tests/basic-types/pointers-arithmetic.vala
new file mode 100644
index 000000000..79437f087
--- /dev/null
+++ b/tests/basic-types/pointers-arithmetic.vala
@@ -0,0 +1,22 @@
+void test_chars () {
+ char* s = "foo";
+ char* begin = s;
+ char* end = begin + 2;
+
+ assert (begin[0] == 'f');
+ assert (end[0] == 'o');
+}
+
+void test_strings () {
+ string s = "foo";
+ string* begin = s;
+ string* end = begin + s.length - 1;
+
+ assert (((char*) begin)[0] == 'f');
+ assert (((char*) end)[0] == 'o');
+}
+
+void main () {
+ test_chars ();
+ test_strings ();
+}
diff --git a/tests/nullability/string-concat.test b/tests/nullability/string-concat.test
new file mode 100644
index 000000000..5cc78efc6
--- /dev/null
+++ b/tests/nullability/string-concat.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+ string? foo = null;
+ string bar = foo + "bar";
+}
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index 7b9b78214..f831c7cb7 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -350,11 +350,11 @@ public class Vala.BinaryExpression : Expression {
right.target_type = right.value_type.copy ();
right.target_type.value_owned = false;
- if (operator == BinaryOperator.PLUS
- && left.value_type.type_symbol == context.analyzer.string_type.type_symbol) {
+ if (operator == BinaryOperator.PLUS && !(left.value_type is PointerType)
+ && left.value_type.compatible (context.analyzer.string_type)) {
// string concatenation
- if (right.value_type == null || right.value_type.type_symbol != context.analyzer.string_type.type_symbol) {
+ if (right.value_type == null || !right.value_type.compatible (context.analyzer.string_type)) {
error = true;
Report.error (source_reference, "Operands must be strings");
return false;