]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Cast initializer-list to struct for non-constant/non-array assignments 68db26a5a26338ce2ccb586d4a7eb109f4a3bbcf
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 17 Jun 2020 17:26:55 +0000 (19:26 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 18 Jun 2020 08:56:47 +0000 (10:56 +0200)
Avoid invalid c-code and use the correct syntax for compound literals.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/1013

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/struct-initializer-list-nested.vala [new file with mode: 0644]

index 97c2617bea36d1e313cb4c79ef8d9dbc8e222118..a159ecfcfccfb8c4cf9b2679c030be4d03781b96 100644 (file)
@@ -2764,7 +2764,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        }
                                }
 
-                               set_cvalue (list, clist);
+                               if (list.parent_node is Constant
+                                   || (list.parent_node is Expression && ((Expression) list.parent_node).value_type is ArrayType)) {
+                                       set_cvalue (list, clist);
+                               } else {
+                                       set_cvalue (list, new CCodeCastExpression (clist, get_ccode_name (list.target_type.type_symbol)));
+                               }
                        } else {
                                // used as expression
                                var instance = create_temp_value (list.value_type, true, list);
index cb930e132e41f8d4fcdff64b393d96b82240712c..aebdf63f4d3e858285da126c4c9427a5fad4a552 100644 (file)
@@ -275,6 +275,7 @@ TESTS = \
        structs/struct-boxed-cast.vala \
        structs/struct-empty-still.test \
        structs/struct-initializer-list-in-array.vala \
+       structs/struct-initializer-list-nested.vala \
        structs/struct-no-gtype.vala \
        structs/struct-no-gtype-inherit.vala \
        structs/struct-static-field-initializer.vala \
diff --git a/tests/structs/struct-initializer-list-nested.vala b/tests/structs/struct-initializer-list-nested.vala
new file mode 100644 (file)
index 0000000..dc5a48f
--- /dev/null
@@ -0,0 +1,63 @@
+struct Foo {
+       int i;
+       int j;
+}
+
+struct Bar {
+       Foo a;
+       Foo? b;
+}
+
+struct Manam {
+       Foo a;
+       Bar b;
+}
+
+struct Baz {
+       Foo f;
+}
+
+const Baz BAZ = { { 23, 42 } };
+
+const Baz[] BAZ_A = { { { 23, 42 } }, { { 47, 11 } } };
+
+void main () {
+       {
+               const Baz LOCAL_BAZ = { { 23, 42 } };
+       }
+       {
+               const Baz[] LOCAL_BAZ_A = { { { 23, 42 } }, { { 47, 11 } } };
+       }
+       {
+               Bar bar = { { 23 , 47 }, { 42, 11 } };
+               assert (bar.a.j == 47);
+               assert (bar.b.i == 42);
+       }
+       {
+               Bar? bar = { { 23 , 47 }, { 42, 11 } };
+               assert (bar.a.i == 23);
+               assert (bar.b.j == 11);
+       }
+       {
+               Bar bar = {};
+               bar = { { 23 , 47 }, { 42, 11 } };
+               assert (bar.a.j == 47);
+               assert (bar.b.i == 42);
+       }
+       {
+               Manam manam = { { 23, 42 }, { { 23 , 47 }, { 42, 11 } } };
+               assert (manam.a.i == 23);
+               assert (manam.b.b.j == 11);
+       }
+       {
+               Manam manam = {};
+               manam = { { 23, 42 }, { { 23 , 47 }, { 42, 11 } } };
+               assert (manam.a.i == 23);
+               assert (manam.b.b.j == 11);
+       }
+       {
+               Manam? manam = { { 23, 42 }, { { 23 , 47 }, { 42, 11 } } };
+               assert (manam.a.j == 42);
+               assert (manam.b.a.i == 23);
+       }
+}