]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Append initializer for "_*_size_" of array field in internal struct 74725956b2c99da3ff289ff2d294c525ff2f85c9
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 25 Feb 2020 11:35:15 +0000 (12:35 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 25 Mar 2020 12:02:55 +0000 (13:02 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/914

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/arrays/struct-field-initializer.vala [new file with mode: 0644]

index 85ea40d5fdc979716a619e96f2569e11abf0ed5a..536aca2f611a7d685b6512c1f53e2bf166636bd8 100644 (file)
@@ -2723,6 +2723,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                                for (int dim = 1; dim <= array_type.rank; dim++) {
                                                        clist.append (get_array_length_cvalue (expr.target_value, dim));
                                                }
+                                               if (array_type.rank == 1 && field.is_internal_symbol ()) {
+                                                       clist.append (get_array_length_cvalue (expr.target_value, 1));
+                                               }
                                        }
                                }
 
index 0eaa2376a8317a164831594313ce769ac2d676d0..7a6d37d8fae8cdee0328428e080c2cc149cbae09 100644 (file)
@@ -90,6 +90,7 @@ TESTS = \
        arrays/length-inline-assignment.vala \
        arrays/length-type-include.vala \
        arrays/struct-field-length-cname.vala \
+       arrays/struct-field-initializer.vala \
        arrays/incompatible-integer-elements.test \
        arrays/slice-invalid-start.test \
        arrays/slice-invalid-stop.test \
diff --git a/tests/arrays/struct-field-initializer.vala b/tests/arrays/struct-field-initializer.vala
new file mode 100644 (file)
index 0000000..9534c84
--- /dev/null
@@ -0,0 +1,33 @@
+[CCode (has_type_id = false)]
+struct Foo {
+       public unowned string[] array;
+       public int i;
+}
+
+[CCode (has_type_id = false)]
+public struct Bar {
+       public unowned string[] array;
+       public int i;
+}
+
+const string[] SARRAY = { "foo", "bar" };
+
+const Foo FOO = { SARRAY, 23 };
+const Bar BAR = { SARRAY, 42 };
+
+void main () {
+       {
+               assert (FOO.array.length == 2);
+               assert (FOO.i == 23);
+               assert (BAR.array.length == 2);
+               assert (BAR.i == 42);
+       }
+       {
+               const Foo foo = { SARRAY, 23 };
+               const Bar bar = { SARRAY, 42 };
+               assert (foo.array.length == 2);
+               assert (foo.i == 23);
+               assert (bar.array.length == 2);
+               assert (bar.i == 42);
+       }
+}