[vala/staging] codegen: Add cast to struct initializers of fields
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] codegen: Add cast to struct initializers of fields
- Date: Wed, 17 Jun 2020 18:19:47 +0000 (UTC)
commit 6db3f95fe4913ca19aa5fdc629cb91eabcae8797
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Wed Jun 17 19:26:55 2020 +0200
codegen: Add cast to struct initializers of fields
Avoid invalid c-code and use the correct syntax for compound literals.
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1013
codegen/valaccodebasemodule.vala | 2 +-
tests/Makefile.am | 1 +
tests/structs/struct-initializer-list-nested.vala | 37 +++++++++++++++++++++++
3 files changed, 39 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 97c2617be..2835faf3b 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2764,7 +2764,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
}
- set_cvalue (list, clist);
+ 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);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cb930e132..aebdf63f4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..0e0a6df5f
--- /dev/null
+++ b/tests/structs/struct-initializer-list-nested.vala
@@ -0,0 +1,37 @@
+struct Foo {
+ int i;
+ int j;
+}
+
+struct Bar {
+ Foo a;
+ Foo? b;
+}
+
+struct Manam {
+ Foo a;
+ Bar b;
+}
+
+void main () {
+ {
+ 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);
+ }
+ {
+ 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]