import std.algorithm, std.typecons;
void main() {
auto t = tuple(1);
swap(t, t);
}
The above program causes an exception in _d_arraycopy:
object.Exception@src/rt/arraycat.d(40): overlapping array copy
----------------
5 y 0x000154c4 _d_arraycopy + 200
6 y 0x000026b5 pure nothrow @trusted void std.algorithm.swap!(std.typecons.Tuple!(int).Tuple).swap(ref std.typecons.Tuple!(int).Tuple, ref std.typecons.Tuple!(int).Tuple) + 169
7 y 0x00001ee9 _Dmain + 33
8 y 0x00015edf extern (C) int rt.dmain2.main(int, char**).void runMain() + 23
9 y 0x00015e66 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 38
10 y 0x00015f27 extern (C) int rt.dmain2.main(int, char**).void runAll() + 59
11 y 0x00015e66 extern (C) int rt.dmain2.main(int, char**).void tryExec(scope void delegate()) + 38
12 y 0x00015df7 main + 179
13 y 0x00001ebd start + 53
14 ??? 0x00000001 0x0 + 1
It should do nothing instead of throwing this exception. As a result of this bug, it is impossible to sort() on an array of tuples.
This bug is absent at least since 2.042 and present on or before 2.052.
Comment #1 by kennytm — 2011-03-05T10:15:59Z
This could probably be fixed/worked-around in druntime.
diff --git a/src/rt/arraycat.d b/src/rt/arraycat.d
index c0aaa4d..e9c2c85 100644
--- a/src/rt/arraycat.d
+++ b/src/rt/arraycat.d
@@ -35,7 +35,7 @@ byte[] _d_arraycopy(size_t size, byte[] from, byte[] to)
{
memcpy(to.ptr, from.ptr, to.length * size);
}
- else
+ else if (to.ptr != from.ptr)
{
throw new Exception("overlapping array copy");
}
Comment #2 by kennytm — 2011-03-08T04:22:42Z
*** Issue 5716 has been marked as a duplicate of this issue. ***
Comment #3 by kennytm — 2011-04-18T12:41:34Z
The the reason is Tuple has an opAssign. The same reason goes for SysTime (issue 5853). Generalized test case:
-------------------------------
import std.algorithm : swap;
struct X {
int x;
void opAssign(X z) {
x = z.x;
}
}
void main() {
X y;
swap(y, y);
}
-------------------------------
Comment #4 by kennytm — 2011-04-18T12:45:36Z
*** Issue 5853 has been marked as a duplicate of this issue. ***
Comment #5 by kennytm — 2011-06-02T15:40:53Z
*** Issue 6093 has been marked as a duplicate of this issue. ***
Comment #6 by code — 2011-06-18T20:07:45Z
*** Issue 4789 has been marked as a duplicate of this issue. ***