[vala/0.38] vala: Properly handle array-initializers inside struct-initializers



commit 8c5e32ffda009cfc00221106cc6352c8e89c9c80
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Feb 15 15:22:35 2018 +0100

    vala: Properly handle array-initializers inside struct-initializers
    
    https://bugzilla.gnome.org/show_bug.cgi?id=622178

 tests/Makefile.am                |    1 +
 tests/basic-types/bug622178.vala |   50 ++++++++++++++++++++++++++++++++++++++
 vala/valainitializerlist.vala    |   15 +++++++++--
 3 files changed, 63 insertions(+), 3 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6981da6..6e681af 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,6 +28,7 @@ TESTS = \
        basic-types/bug595751.vala \
        basic-types/bug596637.vala \
        basic-types/bug596785.vala \
+       basic-types/bug622178.vala \
        basic-types/bug632322.vala \
        basic-types/bug643612.vala \
        basic-types/bug644046.vala \
diff --git a/tests/basic-types/bug622178.vala b/tests/basic-types/bug622178.vala
new file mode 100644
index 0000000..c671d60
--- /dev/null
+++ b/tests/basic-types/bug622178.vala
@@ -0,0 +1,50 @@
+struct Foo {
+       int bar;
+       uint8[] data;
+       int baz;
+}
+
+struct Bar {
+       int bar;
+       uint8[,] data;
+       int baz;
+}
+
+struct Manam {
+       unowned string data[2];
+       int idata[2];
+       int bar;
+}
+
+const Manam[] MANAM = {
+       { { "foo", "bar" }, { 4711, 23 }, 42 },
+};
+
+void main () {
+       Foo foo = { 23, { 0, 1, 2, 3 }, 42 };
+       assert (foo.bar == 23);
+       assert (foo.baz == 42);
+       assert (foo.data.length == 4);
+       assert (foo.data[3] == 3);
+
+       Bar bar = { 23, { { 1, 2 }, { 3, 4 }, { 5, 6 } }, 42 };
+       assert (bar.bar == 23);
+       assert (bar.baz == 42);
+       assert (bar.data.length[0] == 3);
+       assert (bar.data.length[1] == 2);
+       assert (bar.data[2,0] == 5);
+
+       (unowned string)[] sa = { "foo", "bar" };
+       Manam manam = { sa, { 4711, 23 }, 42 };
+       assert (manam.data.length == 2);
+       assert (manam.data[1] == "bar");
+       assert (manam.idata.length == 2);
+       assert (manam.idata[1] == 23);
+       assert (manam.bar == 42);
+
+       assert (MANAM[0].data.length == 2);
+       assert (MANAM[0].data[1] == "bar");
+       assert (MANAM[0].idata.length == 2);
+       assert (MANAM[0].idata[1] == 23);
+       assert (MANAM[0].bar == 42);
+}
diff --git a/vala/valainitializerlist.vala b/vala/valainitializerlist.vala
index c1f97ca..9a18b71 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -129,9 +129,18 @@ public class Vala.InitializerList : Expression {
                        /* initializer is used as array initializer */
                        var array_type = (ArrayType) target_type;
 
-                       if (!(parent_node is ArrayCreationExpression)
-                             && !(parent_node is Constant)
-                             && !(parent_node is InitializerList)) {
+                       bool requires_constants_only = false;
+                       unowned CodeNode? node = parent_node;
+                       while (node != null) {
+                               if (node is Constant) {
+                                       requires_constants_only = true;
+                                       break;
+                               }
+                               node = node.parent_node;
+                       }
+
+                       if (!(parent_node is ArrayCreationExpression) && !requires_constants_only
+                           && (!(parent_node is InitializerList) || ((InitializerList) 
parent_node).target_type.data_type is Struct)) {
                                // transform shorthand form
                                //     int[] array = { 42 };
                                // into


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