dmd
dmd copied to clipboard
`__rvalue` is not working on Windows
Only when the struct has a move constructor will the program have expected behavior (no double free) on Windows. The two examples in DIP draft do not work.
struct MockAllocator
{
enum size_t count = 100;
enum void* baseAddr = cast(void*)0xc0ffee;
static bool[count] freed;
static size_t index;
static void* alloc()
{
void* p = baseAddr + index;
index++;
return index <= count ? p : null;
}
static void free(void* p)
{
if (!p) return;
ptrdiff_t d = p - baseAddr;
assert(d >= 0 && d < count && !freed[d]);
freed[d] = true;
}
}
// Test argument passing
struct S1
{
ubyte* p;
~this()
{
MockAllocator.free(p);
p = null;
}
}
void aggh(S1 s)
{
}
void testArgument()
{
S1 s;
s.p = cast(ubyte*)MockAllocator.alloc();
aggh(__rvalue(s));
}
// Test move assignment
struct S2
{
ubyte* p;
~this()
{
MockAllocator.free(p);
p = null;
}
void opAssign(S2 s)
{
this.p = s.p;
s.p = null;
}
}
void testMoveAssign()
{
S2 s;
s.p = cast(ubyte*)MockAllocator.alloc();
S2 t;
t = __rvalue(s);
}