Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 13300 - pure function 'std.array.Appender!(T[]).Appender.en… #6811

Merged
merged 2 commits into from
Dec 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions std/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -3018,7 +3018,7 @@ if (isDynamicArray!A)
* Params:
* newCapacity = the capacity the `Appender` should have
*/
void reserve(size_t newCapacity) @safe pure nothrow
void reserve(size_t newCapacity)
{
if (_data)
{
Expand Down Expand Up @@ -3053,7 +3053,7 @@ if (isDynamicArray!A)
}

// ensure we can add nelems elements, resizing as necessary
private void ensureAddable(size_t nelems) @trusted pure nothrow
private void ensureAddable(size_t nelems)
{
if (!_data)
_data = new Data;
Expand Down Expand Up @@ -3089,7 +3089,7 @@ if (isDynamicArray!A)
// first, try extending the current block
if (_data.canExtend)
{
immutable u = GC.extend(_data.arr.ptr, nelems * T.sizeof, (newlen - len) * T.sizeof);
immutable u = (() @trusted => GC.extend(_data.arr.ptr, nelems * T.sizeof, (newlen - len) * T.sizeof))();
if (u)
{
// extend worked, update the capacity
Expand All @@ -3105,12 +3105,12 @@ if (isDynamicArray!A)
const nbytes = mulu(newlen, T.sizeof, overflow);
if (overflow) assert(0);

auto bi = GC.qalloc(nbytes, blockAttribute!T);
auto bi = (() @trusted => GC.qalloc(nbytes, blockAttribute!T))();
_data.capacity = bi.size / T.sizeof;
import core.stdc.string : memcpy;
if (len)
memcpy(bi.base, _data.arr.ptr, len * T.sizeof);
_data.arr = (cast(Unqual!T*) bi.base)[0 .. len];
() @trusted { memcpy(bi.base, _data.arr.ptr, len * T.sizeof); }();
_data.arr = (() @trusted => (cast(Unqual!T*) bi.base)[0 .. len])();
_data.canExtend = true;
// leave the old data, for safety reasons
}
Expand Down Expand Up @@ -3205,10 +3205,10 @@ if (isDynamicArray!A)
}

// make sure we have enough space, then add the items
@trusted auto bigDataFun(size_t extra)
auto bigDataFun(size_t extra)
{
ensureAddable(extra);
return _data.arr.ptr[0 .. _data.arr.length + extra];
return (() @trusted => _data.arr.ptr[0 .. _data.arr.length + extra])();
}
auto bigData = bigDataFun(items.length);

Expand Down Expand Up @@ -3402,6 +3402,41 @@ if (isDynamicArray!A)
app.put(r[]);
}

@safe unittest // issue 13300
{
static test(bool isPurePostblit)()
{
static if (!isPurePostblit)
static int i;

struct Simple
{
@disable this(); // Without this, it works.
static if (!isPurePostblit)
this(this) { i++; }
else
pure this(this) { }

private:
this(int tmp) { }
}

struct Range
{
@property Simple front() { return Simple(0); }
void popFront() { count++; }
@property empty() { return count < 3; }
size_t count;
}

Range r;
auto a = r.array();
}

static assert(__traits(compiles, () pure { test!true(); }));
static assert(!__traits(compiles, () pure { test!false(); }));
}

//Calculates an efficient growth scheme based on the old capacity
//of data, and the minimum requested capacity.
//arg curLen: The current length
Expand Down