Bug 10356 – AA of structs: opAssign error.

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-14T04:36:00Z
Last change time
2013-09-07T10:07:18Z
Assigned to
nobody
Creator
szabobogdan

Comments

Comment #0 by szabobogdan — 2013-06-14T04:36:57Z
struct Test { string[] keys; Test opAssign(Test rhs){ assert(this.keys !is null); assert(rhs.keys is null); keys ~= rhs.keys; return this; } } void main() { Test[string] data; Test t; data["test"] = t; } The output: core.exception.OutOfMemoryError
Comment #1 by john.loughran.colvin — 2013-06-17T15:49:40Z
Can't duplicate. I'm not sure what those asserts are for, but the first one fails for the obvious reason that the associative array hasn't been initialised. Without the asserts the code runs fine.
Comment #2 by szabobogdan — 2013-06-17T22:04:05Z
(In reply to comment #1) > Can't duplicate. > > I'm not sure what those asserts are for, but the first one fails for the > obvious reason that the associative array hasn't been initialised. > > Without the asserts the code runs fine. I'm sorry... maybe the original program was striped too much... here is a program where i found the bug: import std.stdio; struct Test { string val = ""; string[] key; Test opAssign(Test val){ this.val = val.val; key ~= val.key; return this; } void append(Test t) { val ~= t.val; key ~= t.key; }; } void main() { Test t; t.val = "test"; Test[string] data; Test v; v.val = "asd"; data["test"] = v; writeln(v); writeln(data["test"]); data["test"].append(t); writeln(data["test"].key); writeln("done"); } Thanks!
Comment #3 by john.loughran.colvin — 2013-06-18T07:18:54Z
OK. A minimized test case: struct Test { byte val; //can be anything string[] key; Test opAssign(Test rhs) //if void, no error. { //any attempt to read key here causes a segfault or OutOfMemoryError. key ~= rhs.key; return this; } } void main() { //only fails using associative array, normal assignment is fine. Test[string] data; data["test"] = Test(); } In the meantime, use the signature "void opAssign(Test rhs)" and you'll (probably) be ok.
Comment #4 by k.hara.pg — 2013-09-07T10:07:18Z
*** This issue has been marked as a duplicate of issue 6178 ***