GNOME Bugzilla – Bug 658720
'unowned' is not propagated to .vapi for non-nullable structs
Last modified: 2018-05-22 14:09:12 UTC
In the following code, the 'unowned' flag for the return type of get_struct_1() is not written correctly to the .vapi file: unowned-values.vala ------------------- public struct S { int foo; } S s; public unowned S get_struct_1 () { return s; } public unowned S? get_struct_2 () { return s; } unowned-values.vapi ------------------- /* unowned-values.vapi generated by valac 0.13.4.8-6faa4, do not modify. */ [CCode (cheader_filename = "unowned-values.h")] public struct S { public int foo; } [CCode (cheader_filename = "unowned-values.h")] public static S get_struct_1 (); [CCode (cheader_filename = "unowned-values.h")] public static unowned S? get_struct_2 (); unowned-values.c ---------------- /* unowned-values.c generated by valac 0.13.4.8-6faa4, the Vala compiler * generated from unowned-values.vala, do not modify */ #include <glib.h> #include <glib-object.h> #include <string.h> #define TYPE_S (s_get_type ()) typedef struct _S S; struct _S { gint foo; }; extern S s; S s = {0}; GType s_get_type (void) G_GNUC_CONST; S* s_dup (const S* self); void s_free (S* self); void get_struct_1 (S* result); S* get_struct_2 (void); S* s_dup (const S* self) { S* dup; dup = g_new0 (S, 1); memcpy (dup, self, sizeof (S)); return dup; } void s_free (S* self) { g_free (self); } GType s_get_type (void) { static volatile gsize s_type_id__volatile = 0; if (g_once_init_enter (&s_type_id__volatile)) { GType s_type_id; s_type_id = g_boxed_type_register_static ("S", (GBoxedCopyFunc) s_dup, (GBoxedFreeFunc) s_free); g_once_init_leave (&s_type_id__volatile, s_type_id); } return s_type_id__volatile; } void get_struct_1 (S* result) { S _tmp0_; _tmp0_ = s; *result = _tmp0_; return; } S* get_struct_2 (void) { S* result = NULL; S _tmp0_; _tmp0_ = s; result = &_tmp0_; return result; }
It's non-sense for such a struct, containing only an int. Does it happen if the struct also contain e.g. a string?
Sorry, blonde moment :) The problem persists for for more complex structs, however. FWIW I actually discovered the bug on a function that returns a Value, but it seems to be an issue for any kind of struct. Here's an amended test case: unowned-values.vala ------------------- public struct S { string foo; } S s; public unowned S get_struct_1 () { return s; } public unowned S? get_struct_2 () { return s; } public void init () { s = S(); s.foo = "bar"; } unowned-values.vapi ------------------- /* unowned-values.vapi generated by valac 0.13.4.8-6faa4, do not modify. */ [CCode (cheader_filename = "unowned-values.h")] public struct S { public string foo; } [CCode (cheader_filename = "unowned-values.h")] public static S get_struct_1 (); [CCode (cheader_filename = "unowned-values.h")] public static unowned S? get_struct_2 (); [CCode (cheader_filename = "unowned-values.h")] public static void init (); unowned-values.c ---------------- /* unowned-values.c generated by valac 0.13.4.8-6faa4, the Vala compiler * generated from unowned-values.vala, do not modify */ #include <glib.h> #include <glib-object.h> #include <stdlib.h> #include <string.h> #define TYPE_S (s_get_type ()) typedef struct _S S; #define _g_free0(var) (var = (g_free (var), NULL)) struct _S { gchar* foo; }; extern S s; S s = {0}; GType s_get_type (void) G_GNUC_CONST; S* s_dup (const S* self); void s_free (S* self); void s_copy (const S* self, S* dest); void s_destroy (S* self); void get_struct_1 (S* result); S* get_struct_2 (void); void init (void); void s_copy (const S* self, S* dest) { const gchar* _tmp0_; const gchar* _tmp1_; _tmp0_ = (*self).foo; _tmp1_ = g_strdup (_tmp0_); _g_free0 ((*dest).foo); (*dest).foo = _tmp1_; } void s_destroy (S* self) { _g_free0 ((*self).foo); } S* s_dup (const S* self) { S* dup; dup = g_new0 (S, 1); s_copy (self, dup); return dup; } void s_free (S* self) { s_destroy (self); g_free (self); } GType s_get_type (void) { static volatile gsize s_type_id__volatile = 0; if (g_once_init_enter (&s_type_id__volatile)) { GType s_type_id; s_type_id = g_boxed_type_register_static ("S", (GBoxedCopyFunc) s_dup, (GBoxedFreeFunc) s_free); g_once_init_leave (&s_type_id__volatile, s_type_id); } return s_type_id__volatile; } void get_struct_1 (S* result) { S _tmp0_; _tmp0_ = s; *result = _tmp0_; return; } S* get_struct_2 (void) { S* result = NULL; S _tmp0_; _tmp0_ = s; result = &_tmp0_; return result; } void init (void) { S _tmp0_ = {0}; gchar* _tmp1_; memset (&_tmp0_, 0, sizeof (S)); s_destroy (&s); s = _tmp0_; _tmp1_ = g_strdup ("bar"); _g_free0 (s.foo); s.foo = _tmp1_; } The 'unowned' attribute is still lost for get_struct_1() in the .vapi ..
Created attachment 196206 [details] [review] Fix DataType.is_weak for struct types This is the fix, but before pushing all the bindings must be fixed accordingly
Thanks, that was quick! What exactly is required to fix the bindings?
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/230.