]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: SliceExpression need to return heap-allocated or unowned references
authorUlrich Küttler <kuettler@gmail.com>
Tue, 22 Dec 2020 07:59:16 +0000 (08:59 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 3 Jan 2021 12:24:53 +0000 (13:24 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1120

tests/Makefile.am
tests/arrays/slice-fixed-length.vala [new file with mode: 0644]
vala/valasliceexpression.vala

index bd8f62d7ef3dcad0d75c4237473f0ecf43b0b2a9..669dccecb4aa4de371afe4ba9f8cf4fd4a84bc65 100644 (file)
@@ -101,6 +101,7 @@ TESTS = \
        arrays/resize-unowned-invalid.test \
        arrays/resize-unowned-invalid-2.test \
        arrays/resize-unowned-invalid-3.test \
+       arrays/slice-fixed-length.vala \
        arrays/slice-invalid-start.test \
        arrays/slice-invalid-stop.test \
        arrays/slice-no-array.test \
diff --git a/tests/arrays/slice-fixed-length.vala b/tests/arrays/slice-fixed-length.vala
new file mode 100644 (file)
index 0000000..21e9c6d
--- /dev/null
@@ -0,0 +1,69 @@
+void manam (string[] foo) {
+       assert (foo.length == 2);
+       assert (foo[0] == "bar");
+       assert (foo[1] == "baz");
+}
+
+void minim (owned string[] foo) {
+       assert (foo.length == 2);
+       assert (foo[0] == "bar");
+       assert (foo[1] == "baz");
+}
+
+void main () {
+       string bar[4] = { "foo", "bar", "baz", "buzz" };
+       {
+               var foo = bar[1:3];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+       {
+               unowned string[] foo = bar[1:3];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+       {
+               int begin = 1;
+               var foo = bar[begin:3];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+       {
+               string[] foo = bar[1:3];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+       {
+               int end = 3;
+               string[] foo = bar[1:end];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+       {
+               manam (bar[1:3]);
+       }
+       {
+               int begin = 1;
+               manam (bar[begin:3]);
+       }
+       {
+               int end = 3;
+               manam (bar[1:end]);
+       }
+       {
+               minim (bar[1:3]);
+       }
+       {
+               int begin = 1;
+               minim (bar[begin:3]);
+       }
+       {
+               int end = 3;
+               minim (bar[1:end]);
+       }
+}
index bc399c8fcb3aa8eac12d9b4160efc0b4d4e9e1fd..0a4110e17f143bbd2a2d1ec6eebb993049791d60 100644 (file)
@@ -147,6 +147,12 @@ public class Vala.SliceExpression : Expression {
                        value_type = container.value_type.copy ();
                        value_type.value_owned = false;
 
+                       // inline allocated results are not compatible with non-constant start/stop expressions
+                       unowned ArrayType array_type = (ArrayType) value_type;
+                       array_type.fixed_length = false;
+                       array_type.inline_allocated = false;
+                       array_type.length = null;
+
                        /* check if the index is of type integer */
                        if (!(start.value_type is IntegerType || start.value_type is EnumValueType)) {
                                error = true;