From be7c1e5e4a0e239006c982969e2b054738bf3cdd Mon Sep 17 00:00:00 2001 From: Sergii Kuzko Date: Wed, 22 Apr 2026 12:31:13 +0300 Subject: [PATCH] sync root_array_d_file --- compiler/src/dmd/root/array.d | 61 +++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/compiler/src/dmd/root/array.d b/compiler/src/dmd/root/array.d index f36ddb4f29e..1ec25369fe7 100644 --- a/compiler/src/dmd/root/array.d +++ b/compiler/src/dmd/root/array.d @@ -16,7 +16,7 @@ import core.stdc.stdlib : _compare_fp_t; import core.stdc.string; import dmd.root.rmem; -import dmd.root.string; +import dmd.root.string : toDString; // `qsort` is only `nothrow` since 2.081.0 private extern(C) void qsort(scope void* base, size_t nmemb, size_t size, _compare_fp_t compar) nothrow @nogc; @@ -51,10 +51,26 @@ public: ~this() pure nothrow { - debug (stomp) memset(data.ptr, 0xFF, data.length); - if (data.ptr != &smallarray[0]) + debug (stomp) + { + if (data.ptr) + memset(data.ptr, 0xFF, data.length * T.sizeof); + } + if (data.ptr && data.ptr != &smallarray[0]) mem.xfree(data.ptr); } + + // this is using a template constraint because of ambiguity with this(size_t) when T is + // int, and c++ header generation doesn't accept wrapping this in static if + extern(D) this()(T[] elems ...) pure nothrow if (is(T == struct) || is(T == class)) + { + this(elems.length); + foreach(i; 0 .. elems.length) + { + this[i] = elems[i]; + } + } + ///returns elements comma separated in [] extern(D) const(char)[] toString() const { @@ -189,22 +205,33 @@ public: memcpy(p, data.ptr, length * T.sizeof); memset(data.ptr, 0xFF, data.length * T.sizeof); mem.xfree(data.ptr); + data = p[0 .. allocdim]; } else + { auto p = cast(T*)mem.xrealloc(data.ptr, allocdim * T.sizeof); - data = p[0 .. allocdim]; + data = p[0 .. allocdim]; + } } debug (stomp) { - if (length < data.length) - memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof); + if (data.ptr) + { + if (length < data.length) + memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof); + } } else { if (mem.isGCEnabled) - if (length < data.length) - memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof); + { + if (data.ptr) + { + if (length < data.length) + memset(data.ptr + length, 0xFF, (data.length - length) * T.sizeof); + } + } } } @@ -896,7 +923,7 @@ bool equal(Range1, Range2)(Range1 range1, Range2 range2) else { - static if (hasLength!Range1 && hasLength!Range2 && is(typeof(r1.length == r2.length))) + static if (hasLength!Range1 && hasLength!Range2 && is(typeof(range1.length == range2.length))) { if (range1.length != range2.length) return false; @@ -1181,3 +1208,19 @@ pure nothrow @nogc @safe unittest b.popFront(); assert(b == expected[]); } + + +/// Test Array array constructor +pure nothrow unittest +{ + //check to make sure that this works with the aliases in arraytypes.d + import dmd.rootobject; + alias Objects = Array!RootObject; + + auto ro1 = new RootObject(); + auto ro2 = new RootObject(); + + auto aoo = new Objects(ro1, ro2); + assert((*aoo)[0] is ro1); + assert((*aoo)[1] is ro2); +}