[vala/0.46] vala: Correctly handle qualified struct type reference in initializer list



commit 2db90f2d206d7e067aae077e366921de862f5344
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Jun 6 09:53:19 2020 +0200

    vala: Correctly handle qualified struct type reference in initializer list
    
    Construct member-access including namespace of inferred struct type.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1004

 tests/Makefile.am                               |  1 +
 tests/arrays/struct-namespaced-initializer.vala | 27 +++++++++++++++++++++++++
 vala/valainitializerlist.vala                   | 12 +++++++++--
 3 files changed, 38 insertions(+), 2 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5663d7678..02a0292ab 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -97,6 +97,7 @@ TESTS = \
        arrays/length-type-include.vala \
        arrays/struct-field-length-cname.vala \
        arrays/struct-field-initializer.vala \
+       arrays/struct-namespaced-initializer.vala \
        arrays/incompatible-integer-elements.test \
        arrays/slice-invalid-start.test \
        arrays/slice-invalid-stop.test \
diff --git a/tests/arrays/struct-namespaced-initializer.vala b/tests/arrays/struct-namespaced-initializer.vala
new file mode 100644
index 000000000..e1c5ad47b
--- /dev/null
+++ b/tests/arrays/struct-namespaced-initializer.vala
@@ -0,0 +1,27 @@
+namespace Manam {
+       namespace Bar {
+               public struct Foo {
+                       public int i;
+                       public int j;
+               }
+       }
+
+       public class Baz {
+               public struct Faz {
+                       public string s;
+                       public uint i;
+               }
+       }
+}
+
+void main () {
+       Manam.Bar.Foo[] foo = { { 42, 4711 }, { 23, 17 } };
+       assert (foo.length == 2);
+       assert (foo[0].i == 42);
+       assert (foo[1].j == 17);
+
+       Manam.Baz.Faz[] faz = { { "manam", 4711U } };
+       assert (faz.length == 1);
+       assert (faz[0].s == "manam");
+       assert (faz[0].i == 4711U);
+}
diff --git a/vala/valainitializerlist.vala b/vala/valainitializerlist.vala
index 720a7ccec..ac35abc01 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -197,9 +197,17 @@ public class Vala.InitializerList : Expression {
                        var in_array_creation_initializer = parent_node is InitializerList && 
parent_node.parent_node is ArrayCreationExpression;
                        ObjectCreationExpression? struct_creation = null;
                        if (in_array_creation_initializer) {
-                               var ma = new MemberAccess.simple (st.name, source_reference);
+                               unowned Symbol? sym = st;
+                               var ma = new MemberAccess.simple (sym.name, source_reference);
                                ma.creation_member = true;
-                               ma.symbol_reference = st;
+                               ma.symbol_reference = sym;
+                               MemberAccess inner = ma;
+                               while (sym.parent_symbol != null && sym.parent_symbol != context.root) {
+                                       sym = sym.parent_symbol;
+                                       var ma_inner = new MemberAccess.simple (sym.name, source_reference);
+                                       inner.inner = ma_inner;
+                                       inner = ma_inner;
+                               }
                                struct_creation = new ObjectCreationExpression (ma, source_reference);
                                struct_creation.target_type = target_type.copy ();
                                struct_creation.struct_creation = true;


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