[vala/staging] codegen: Cast given default-value of struct with possible member initializer
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/staging] codegen: Cast given default-value of struct with possible member initializer
- Date: Mon, 3 Jan 2022 13:00:24 +0000 (UTC)
commit 29470dbcfc149d241886dcecb7fd6f4207dc7917
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Jan 3 13:59:36 2022 +0100
codegen: Cast given default-value of struct with possible member initializer
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1272
codegen/valaccodebasemodule.vala | 6 ++-
tests/structs/simple-type-default-value.c-expected | 56 ++++++++++++++++++++++
tests/structs/simple-type-default-value.vala | 14 ++++++
3 files changed, 75 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 2e7df70d5..db9a498cc 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6544,7 +6544,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.c-expected
b/tests/structs/simple-type-default-value.c-expected
new file mode 100644
index 000000000..a9a58bbab
--- /dev/null
+++ b/tests/structs/simple-type-default-value.c-expected
@@ -0,0 +1,56 @@
+/* structs_simple_type_default_value.c generated by valac, the Vala compiler
+ * generated from structs_simple_type_default_value.vala, do not modify */
+
+#include <glib.h>
+#include <glib-object.h>
+
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+typedef struct _Foo Foo;
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__,
__LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN,
G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning
(G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__,
__LINE__, G_STRFUNC, msg);
+
+struct _Foo {
+ gint i;
+};
+
+VALA_EXTERN Foo bar (GObject* o);
+static void _vala_main (void);
+
+Foo
+bar (GObject* o)
+{
+ Foo _tmp0_ = (Foo) { .i = 23 };
+ Foo result = (Foo) { .i = 23 };
+ g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (o, G_TYPE_OBJECT), (Foo) { .i = 23 });
+ result = _tmp0_;
+ return result;
+}
+
+static void
+_vala_main (void)
+{
+ Foo foo = (Foo) { .i = 23 };
+ Foo _tmp0_ = (Foo) { .i = 23 };
+ foo = _tmp0_;
+ _vala_assert (foo.i == 23, "foo.i == 23");
+}
+
+int
+main (int argc,
+ char ** argv)
+{
+ _vala_main ();
+ return 0;
+}
+
diff --git a/tests/structs/simple-type-default-value.vala b/tests/structs/simple-type-default-value.vala
new file mode 100644
index 000000000..63c21bdd2
--- /dev/null
+++ b/tests/structs/simple-type-default-value.vala
@@ -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);
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]