Nullable SimpleType structs result in C compiler errors
The following short snippet demonstrates the problem:
[SimpleType]
public struct SimpleData {
uint8 content;
}
var? t = SimpleData() { content = 42 };
print("Okay, %u\n", t.content);
Trying to compile this produces following C code:
/* vala_playground.c generated by valac 0.54.5, the Vala compiler
* generated from vala_playground.vala, do not modify */
#include <glib.h>
#include <string.h>
typedef struct _SimpleData SimpleData;
#define _simple_data_free0(var) ((var == NULL) ? NULL : (var = (simple_data_free (var), NULL)))
struct _SimpleData {
guint8 content;
};
static void _vala_main (void);
static gpointer
_simple_data_dup0 (gpointer self)
{
return self ? simple_data_dup (self) : NULL;
}
static void
_vala_main (void)
{
SimpleData* t = NULL;
SimpleData _tmp0_ = {0};
SimpleData* _tmp1_;
memset (&_tmp0_, 0, sizeof (SimpleData));
_tmp0_.content = (guint8) 42;
_tmp1_ = _simple_data_dup0 (&_tmp0_);
t = _tmp1_;
g_print ("Okay, %u\n", (guint) (*t).content);
_simple_data_free0 (t);
}
int
main (int argc,
char ** argv)
{
_vala_main ();
return 0;
}
This fails to compile/link due to undefined simple_data_free
and simple_data_dup
. Since SimpleType
structs don't need special cleanup on destruction, it should be possible to:
- Make
_simple_data_dup0
a simple function that callsg_malloc
andmemcpy
. - Make
_simple_data_free0
macro just callg_free
in place ofsimple_data_free
.