]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Cast given default-value of struct with possible member initializer
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 3 Jan 2022 12:59:36 +0000 (13:59 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 4 Jan 2022 11:29:02 +0000 (12:29 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1272

codegen/valaccodebasemodule.vala
tests/structs/simple-type-default-value.vala [new file with mode: 0644]

index f032ecab37c930d23e9305e286a54f486d20cff4..b8be34a62e25dbd7bbfe172736e158d46868c378 100644 (file)
@@ -6459,7 +6459,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                var array_type = type as ArrayType;
                if (type.type_symbol != null && !type.nullable
                    && (on_error ? get_ccode_default_value_on_error (type.type_symbol) : get_ccode_default_value (type.type_symbol)) != "") {
-                       return new CCodeConstant (on_error ? get_ccode_default_value_on_error (type.type_symbol) : get_ccode_default_value (type.type_symbol));
+                   CCodeExpression val = new CCodeConstant (on_error ? get_ccode_default_value_on_error (type.type_symbol) : get_ccode_default_value (type.type_symbol));
+                   if (st != null && st.get_fields ().size > 0) {
+                               val = new CCodeCastExpression (val, get_ccode_name (st));
+                       }
+                       return val;
                } else if (initializer_expression && !type.nullable &&
                                   (st != null || (array_type != null && array_type.fixed_length))) {
                        // 0-initialize struct with struct initializer { 0 }
diff --git a/tests/structs/simple-type-default-value.vala b/tests/structs/simple-type-default-value.vala
new file mode 100644 (file)
index 0000000..63c21bd
--- /dev/null
@@ -0,0 +1,14 @@
+[CCode (default_value = "{ .i = 23 }")]
+[SimpleType]
+public struct Foo {
+       int i;
+}
+
+Foo bar (Object o) {
+       return {};
+}
+
+void main () {
+       Foo foo = {};
+       assert (foo.i == 23);
+}