Bug 6436 – Refcounted initialization bug

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-08-04T13:35:00Z
Last change time
2013-02-05T13:19:58Z
Assigned to
verylonglogin.reg
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2011-08-04T13:35:13Z
import std.typecons; struct Foo { struct Payload { this(ref int bar, int value) { bar = value; assert(bar == 5); // ok } } alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data; Data data; int bar; this(int value) { data = Data(bar, value); assert(bar == 5); // throws, it's still 0 } } void main() { auto foo = Foo(5); } import std.typecons; struct Foo { struct Payload { this(ref int bar, int value) { bar = value; assert(bar == 5); // ok.. } } alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data; Data data; int bar; this(int value) { data = Data(bar, value); // initializes bar to 5 assert(bar == 5); // throws, it's still 0 } } void main() { auto foo = Foo(5); } Foo's ctor is called, then Refcounted Payload's ctor is called which initializes Foo's integer field, but upon return to the Foo ctor the integer field seems to be reinitialized to 0.
Comment #1 by andrej.mitrovich — 2011-08-04T13:36:01Z
Oh damnit duplicated code sorry: import std.typecons; struct Foo { struct Payload { this(ref int bar, int value) { bar = value; assert(bar == 5); // ok } } alias RefCounted!(Payload, RefCountedAutoInitialize.yes) Data; Data data; int bar; this(int value) { data = Data(bar, value); assert(bar == 5); // throws, it's still 0 } } void main() { auto foo = Foo(5); }
Comment #2 by andrej.mitrovich — 2012-04-19T18:45:56Z
Interestingly if you pass by pointer instead of by ref it works fine. Here's a much more simplified example: import std.typecons; import std.stdio; struct Struct1 { this(ref int val) { val = 5; } } struct Struct2 { this(int* val) { *val = 5; } } alias RefCounted!(Struct1) Str1; alias RefCounted!(Struct2) Str2; void main() { Str1 str1; Str2 str2; int val; str1 = Str1(val); writeln(val); // writes 0 str2 = Str2(&val); writeln(val); // writes 5 }
Comment #3 by verylonglogin.reg — 2012-10-26T09:21:00Z
This issue is blocked by `emplace` issue which is fixed in pull 896: https://github.com/D-Programming-Language/phobos/pull/896
Comment #4 by verylonglogin.reg — 2012-10-26T09:47:37Z
Comment #5 by github-bugzilla — 2012-12-12T07:41:14Z
Comment #6 by andrej.mitrovich — 2013-02-05T13:19:58Z
Thanks, fixed.