[vala/staging] vala: Set length of SliceExpression for fixed length array results



commit c17058dc3bc0856acbfb09020ecf5f8c3dc5c929
Author: Ulrich Küttler <kuettler gmail com>
Date:   Tue Dec 22 08:59:16 2020 +0100

    vala: Set length of SliceExpression for fixed length array results
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1120

 tests/Makefile.am                    |  1 +
 tests/arrays/slice-fixed-length.vala | 15 +++++++++++++++
 vala/valasliceexpression.vala        |  7 ++++++-
 3 files changed, 22 insertions(+), 1 deletion(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ddac20684..e4a305f21 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -128,6 +128,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
index 000000000..30f36eee3
--- /dev/null
+++ b/tests/arrays/slice-fixed-length.vala
@@ -0,0 +1,15 @@
+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");
+       }
+       {
+               string[] foo = bar[1:3];
+               assert (foo.length == 2);
+               assert (foo[0] == "bar");
+               assert (foo[1] == "baz");
+       }
+}
diff --git a/vala/valasliceexpression.vala b/vala/valasliceexpression.vala
index 8eca80998..aa643f75b 100644
--- a/vala/valasliceexpression.vala
+++ b/vala/valasliceexpression.vala
@@ -147,7 +147,6 @@ public class Vala.SliceExpression : Expression {
                if (container.value_type is ArrayType) {
                        value_type = container.value_type.copy ();
                        value_type.value_owned = false;
-                       value_type.check (context);
 
                        /* check if the index is of type integer */
                        if (!(start.value_type is IntegerType || start.value_type is EnumValueType)) {
@@ -158,6 +157,12 @@ public class Vala.SliceExpression : Expression {
                                error = true;
                                Report.error (stop.source_reference, "Expression of integer type expected");
                        }
+
+                       unowned ArrayType array_type = (ArrayType) value_type;
+                       if (array_type.fixed_length) {
+                               array_type.length = new BinaryExpression (BinaryOperator.MINUS, stop, start);
+                       }
+                       value_type.check (context);
                } else {
                        var slice_method = container.value_type.get_member ("slice") as Method;
                        if (slice_method != null) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]