D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6187 - compiler could use move semantics for appending temporaries to an array
Summary: compiler could use move semantics for appending temporaries to an array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-21 04:52 UTC by Steven Schveighoffer
Modified: 2015-12-10 14:46 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Steven Schveighoffer 2011-06-21 04:52:58 UTC
Currently, the following code:

import std.stdio;

struct S
{
   int x;
   this(this) { writeln("postblit"); }
   ~this() { writeln("dtor"); }
}

void main()
{
   S[] arr;
   arr ~= S();
}

outputs:

postblit
dtor

But it could have move semantics.  However, the runtime function which appends data does not know whether the given data to append is an rvalue or not.  For example, the following cannot have move semantics:

S s;
s.x = 1;
arr ~= s;
writeln(s.x);

So the compiler must give an additional flag (or call a separate runtime function) when appending an rvalue.  When the compiler does this, flip the bug to a druntime bug and assign it to me, and I'll fix the runtime.
Comment 1 Infiltrator 2015-12-09 12:07:42 UTC
Can I assume that this has been fixed?  2.069 outputs only the "dtor" and no postblit.
Comment 2 Steven Schveighoffer 2015-12-10 14:46:19 UTC
I think this can be closed. The temporary is no longer destructed. The dtor is called actually by the GC.

I tested as far back as 2.067.1