Bug 4356 – Copy constructor not called under extremely mysterious circumstances

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-06-20T20:40:00Z
Last change time
2012-05-23T11:16:51Z
Assigned to
nobody
Creator
andrei

Comments

Comment #0 by andrei — 2010-06-20T20:40:18Z
I spent the better part of today trying to escape this elusive bug. It's extremely subtle because it depends on some unrelated code existing or not. Here it is: import std.stdio; struct X(T) { this(this) { writeln("I was copied"); } } void main() { struct A { X!int x; this(int y) { } A copy() { auto another = this; return another; } } auto a = A(4); auto b = a.copy(); writefln("a: %p, b: %p\n", &a, &b); } At the end of this program the writefln witnesses that we have two objects, but the stdout is mute so the subobject was not copied. Changing some code in the example above makes the example work. For example, replacing A(4) with A() produces a correct program. Please give this priority (as all ctor/dtor bugs should have). Thanks.
Comment #1 by bugzilla — 2010-06-21T02:13:46Z
Simpler test case: import std.c.stdio; struct A { int m; this(this) { printf("this(this) %p\n", &this); } ~this() { printf("~this() %p\n", &this); } A copy() { A another = this; return another; } } void main() { A a; A b = a.copy(); printf("a: %p, b: %p\n", &a, &b); }
Comment #2 by bugzilla — 2010-06-21T22:15:06Z
Comment #3 by github-bugzilla — 2012-05-23T11:16:51Z