Skip to content

Commit

Permalink
fix issue 20937 - std.range.array of a lengthless range with indirect…
Browse files Browse the repository at this point in the history
…ion is not @safe
  • Loading branch information
dukc committed Aug 24, 2021
1 parent 7d739d9 commit c5c5593
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions std/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ if (isPointer!Range && isIterable!(PointerTarget!Range) && !isAutodecodableStrin
)));
}

// https://issues.dlang.org/show_bug.cgi?id=20937
@safe pure nothrow unittest
{
struct S {int* x;}
struct R
{
immutable(S) front;
bool empty;
@safe pure nothrow void popFront(){empty = true;}
}
R().array;
}

/**
Convert a narrow autodecoding string to an array type that fully supports
random access. This is handled as a special case and always returns an array
Expand Down Expand Up @@ -3492,13 +3505,14 @@ if (isDynamicArray!A)
}
else
{
import core.internal.lifetime : emplaceRef;
import core.lifetime : emplace;

ensureAddable(1);
immutable len = _data.arr.length;

auto bigData = (() @trusted => _data.arr.ptr[0 .. len + 1])();
emplaceRef!(Unqual!T)(bigData[len], cast() item);
auto itemUnqual = (() @trusted => & cast() item)();
emplace(&bigData[len], *itemUnqual);
//We do this at the end, in case of exceptions
_data.arr = bigData;
}
Expand Down

0 comments on commit c5c5593

Please sign in to comment.