[vala/0.40] codegen: Cast initializer-list to struct for non-constant/non-array assignments



commit c468319f3724841b3000358501da0d4e516c5c07
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Jun 17 19:26:55 2020 +0200

    codegen: Cast initializer-list to struct for non-constant/non-array assignments
    
    Avoid invalid c-code and use the correct syntax for compound literals.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1013

 codegen/valaccodebasemodule.vala                  |  7 ++-
 tests/Makefile.am                                 |  1 +
 tests/structs/struct-initializer-list-nested.vala | 63 +++++++++++++++++++++++
 3 files changed, 70 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index a9fd428ba..25a7f3309 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2695,7 +2695,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.data_type)));
+                               }
                        } else {
                                // used as expression
                                var instance = create_temp_value (list.value_type, true, list);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 84517abfa..c8ed3214b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -244,6 +244,7 @@ TESTS = \
        structs/struct-base-types.vala \
        structs/struct-boxed-cast.vala \
        structs/struct-initializer-list-in-array.vala \
+       structs/struct-initializer-list-nested.vala \
        structs/structs.vala \
        structs/constructor-wrong-name.test \
        structs/gmutex.vala \
diff --git a/tests/structs/struct-initializer-list-nested.vala 
b/tests/structs/struct-initializer-list-nested.vala
new file mode 100644
index 000000000..dc5a48f21
--- /dev/null
+++ b/tests/structs/struct-initializer-list-nested.vala
@@ -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);
+       }
+}


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