Fix issue 24018: set length manually via runtime function. (#15354)

* Fix issue 24018: make `array.length = n` `@system` if default construction is disabled on the array type.
This is needed as there is otherwise no good way to resize an array of non-constructable elements in user code at all.

* Fix issue 24018: instead of calling `array.length =`, call the runtime function that implements `array.length =` instead.
This skips the check for the element type being constructable, which is correct because concatenation doesn't expose unconstructed memory anyway.
This commit is contained in:
FeepingCreature 2023-06-27 14:51:07 +02:00 committed by GitHub
parent a612593c4d
commit 9bf123c704
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -0,0 +1,10 @@
struct S
{
@disable this();
}
void main()
{
S[] s;
s = s ~ s;
}

View file

@ -19,6 +19,7 @@ module core.internal.array.concatenation;
*/
Tret _d_arraycatnTX(Tret, Tarr...)(auto ref Tarr froms) @trusted
{
import core.internal.array.capacity : _d_arraysetlengthTImpl;
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
import core.lifetime : copyEmplace;
import core.stdc.string : memcpy;
@ -38,7 +39,21 @@ Tret _d_arraycatnTX(Tret, Tarr...)(auto ref Tarr froms) @trusted
if (totalLen == 0)
return res;
res.length = totalLen;
// We cannot use this, because it refuses to work if the array type has disabled default construction.
// res.length = totalLen;
// Call the runtime function directly instead.
// TODO: once `__arrayAlloc` is templated, call that instead.
version (D_ProfileGC)
{
// TODO: forward file, line, name from _d_arraycatnTXTrace
_d_arraysetlengthTImpl!(typeof(res))._d_arraysetlengthTTrace(
__FILE__, __LINE__, "_d_arraycatnTX", res, totalLen);
}
else
{
_d_arraysetlengthTImpl!(typeof(res))._d_arraysetlengthT(res, totalLen);
}
/* Currently, if both a postblit and a cpctor are defined, the postblit is
* used. If this changes, the condition below will have to be adapted.