99*/
1010module core.internal.array.construction ;
1111
12+ import core.internal.traits : Unqual;
13+
1214/**
1315 * Does array initialization (not assignment) from another array of the same element type.
1416 * Params:
15- * to = what array to initialize
1617 * from = what data the array should be initialized with
1718 * Returns:
18- * The constructed `to`
19+ * The created and initialized array `to`
1920 * Bugs:
2021 * This function template was ported from a much older runtime hook that bypassed safety,
2122 * purity, and throwabilty checks. To prevent breaking existing code, this function template
2223 * is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
2324 */
24- Tarr _d_arrayctor (Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
25+ Tarr1 _d_arrayctor (Tarr1 : T1 [], Tarr2 : T2 [], T1 , T2 )(scope Tarr2 from) @trusted
26+ if (is (Unqual! T1 == Unqual! T2 ))
2527{
2628 pragma (inline, false );
27- import core.internal.traits : hasElaborateCopyConstructor, Unqual ;
29+ import core.internal.traits : hasElaborateCopyConstructor;
2830 import core.lifetime : copyEmplace;
2931 import core.stdc.string : memcpy;
3032 import core.stdc.stdint : uintptr_t ;
3133 debug (PRINTF ) import core.stdc.stdio : printf;
3234
33- debug (PRINTF ) printf(" _d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n " , from.ptr, from.length, to.ptr, to.length, T.tsize);
35+ debug (PRINTF ) printf(" _d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n " , from.ptr, from.length, to.ptr, to.length, T1 .tsize);
36+
37+ Tarr1 to = void ;
3438
3539 void [] vFrom = (cast (void * )from.ptr)[0 .. from.length];
3640 void [] vTo = (cast (void * )to.ptr)[0 .. to.length];
@@ -46,13 +50,11 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
4650 (cast (Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false );
4751 }
4852
49- enforceRawArraysConformable(" initialization" , T .sizeof, vFrom, vTo);
53+ enforceRawArraysConformable(" initialization" , T1 .sizeof, vFrom, vTo);
5054
51- static if (hasElaborateCopyConstructor! T )
55+ static if (hasElaborateCopyConstructor! T1 )
5256 {
53- // Use uint instead of size_t as a temporary workaround until this bug is fixed:
54- // https://issues.dlang.org/show_bug.cgi?id=22372
55- uint i;
57+ size_t i;
5658 try
5759 {
5860 for (i = 0 ; i < to.length; i++ )
@@ -64,7 +66,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
6466 */
6567 while (i-- )
6668 {
67- auto elem = cast (Unqual! T * )&to[i];
69+ auto elem = cast (Unqual! T1 * )&to[i];
6870 destroy (* elem);
6971 }
7072
@@ -74,7 +76,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
7476 else
7577 {
7678 // blit all elements at once
77- memcpy(cast (void * ) to.ptr, from.ptr, to.length * T .sizeof);
79+ memcpy(cast (void * ) to.ptr, from.ptr, to.length * T1 .sizeof);
7880 }
7981
8082 return to;
@@ -92,7 +94,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
9294
9395 S[4 ] arr1;
9496 S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
95- _d_arrayctor( arr1[], arr2[]);
97+ arr1 = _d_arrayctor! ( typeof ( arr1))( arr2[]);
9698
9799 assert (counter == 4 );
98100 assert (arr1 == arr2);
@@ -115,7 +117,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
115117
116118 S[4 ] arr1;
117119 S[4 ] arr2 = [S(0 ), S(1 ), S(2 ), S(3 )];
118- _d_arrayctor( arr1[], arr2[]);
120+ arr1 = _d_arrayctor! ( typeof ( arr1))( arr2[]);
119121
120122 assert (counter == 4 );
121123 assert (arr1 == arr2);
@@ -141,7 +143,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
141143 {
142144 Throw[4 ] a;
143145 Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
144- _d_arrayctor(a[], b[]);
146+ a = _d_arrayctor! ( typeof (a))( b[]);
145147 }
146148 catch (Exception )
147149 {
@@ -166,7 +168,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
166168 {
167169 NoThrow[4 ] a;
168170 NoThrow[4 ] b = [NoThrow(1 ), NoThrow(2 ), NoThrow(3 ), NoThrow(4 )];
169- _d_arrayctor(a[], b[]);
171+ a = _d_arrayctor! ( typeof (a))( b[]);
170172 }
171173 catch (Exception )
172174 {
@@ -190,7 +192,6 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
190192void _d_arraysetctor (Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
191193{
192194 pragma (inline, false );
193- import core.internal.traits : Unqual;
194195 import core.lifetime : copyEmplace;
195196
196197 size_t i;
@@ -273,7 +274,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
273274 {
274275 Throw[4 ] a;
275276 Throw[4 ] b = [Throw(1 ), Throw(2 ), Throw(3 ), Throw(4 )];
276- _d_arrayctor(a[], b[]);
277+ a = _d_arrayctor! ( typeof (a))( b[]);
277278 }
278279 catch (Exception )
279280 {
0 commit comments