Invoking named constructor within construct method does not work with structs
Test case:
class Foo {
public int flag;
public Foo (int flag) {
this.flag = flag;
}
public Foo.one () {
this (1);
}
public Foo.two () {
this.one ();
this.flag++;
}
}
struct Bar {
public int flag;
public Bar (int flag) {
this.flag = flag;
}
public Bar.one () {
this (1);
}
public Bar.two () {
this.one ();
this.flag++;
}
}
void main () {
var foo = new Foo.one ();
var bar = Bar.one ();
assert (foo.flag == 1);
assert (bar.flag == 1);
var foo2 = new Foo.two ();
var bar2 = Bar.two ();
assert (foo2.flag == 2);
assert (bar2.flag == 2);
}
Only the last assert fails. This is because the call to Bar.one
within Bar.two
passes a temporary for self
, but this is never copied back:
void
bar_init_two (Bar *self)
{
Bar _tmp0_ = {0};
gint _tmp1_;
memset (self, 0, sizeof (Bar));
bar_init_one (&_tmp0_);
_tmp1_ = (*self).flag;
(*self).flag = _tmp1_ + 1;
}
The scheme works when the callee is not a named constructor:
void
bar_init_one (Bar *self)
{
memset (self, 0, sizeof (Bar));
bar_init (self, 1);
}