Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 5a4b83b

Browse files
committed
Simplify refactored signature for _d_arrayctor
The current form still does not perform NRVO Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
1 parent c259006 commit 5a4b83b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/core/internal/array/construction.d

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010
module core.internal.array.construction;
1111

12-
import core.internal.traits : Unqual;
13-
1412
/**
1513
* Does array initialization (not assignment) from another array of the same element type.
1614
* Params:
@@ -23,8 +21,7 @@ import core.internal.traits : Unqual;
2321
* purity, and throwabilty checks. To prevent breaking existing code, this function template
2422
* is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
2523
*/
26-
Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t length) @trusted
27-
if (is(Unqual!T1 == Unqual!T2))
24+
T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
2825
{
2926
pragma(inline, false);
3027
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
@@ -33,15 +30,10 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
3330
import core.stdc.stdint : uintptr_t;
3431
debug(PRINTF) import core.stdc.stdio : printf;
3532

36-
debug(PRINTF) printf("_d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, T1.tsize);
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);
3734

38-
Tarr1 to;
39-
// Check if Tarr1 is a dynamic array.
40-
// Otherwise, to.length is already set.
41-
static if (__traits(compiles, {to.length = length;}))
42-
{
43-
to.length = length;
44-
}
35+
T[] to;
36+
to.length = length;
4537

4638
void[] vFrom = (cast(void*)from.ptr)[0..from.length];
4739
void[] vTo = (cast(void*)to.ptr)[0..to.length];
@@ -57,11 +49,13 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
5749
(cast(Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false);
5850
}
5951

60-
enforceRawArraysConformable("initialization", T1.sizeof, vFrom, vTo);
52+
enforceRawArraysConformable("initialization", T.sizeof, vFrom, vTo);
6153

62-
static if (hasElaborateCopyConstructor!T1)
54+
static if (hasElaborateCopyConstructor!T)
6355
{
64-
size_t i;
56+
// Use uint instead of size_t as a temporary workaround until this bug is fixed:
57+
// https://issues.dlang.org/show_bug.cgi?id=22372
58+
uint i;
6559
try
6660
{
6761
for (i = 0; i < to.length; i++)
@@ -73,7 +67,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
7367
*/
7468
while (i--)
7569
{
76-
auto elem = cast(Unqual!T1*)&to[i];
70+
auto elem = cast(Unqual!T*)&to[i];
7771
destroy(*elem);
7872
}
7973

@@ -83,7 +77,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
8377
else
8478
{
8579
// blit all elements at once
86-
memcpy(cast(void*) to.ptr, from.ptr, to.length * T1.sizeof);
80+
memcpy(cast(void*) to.ptr, from.ptr, to.length * T.sizeof);
8781
}
8882

8983
return to;
@@ -101,7 +95,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
10195

10296
S[4] arr1;
10397
S[4] arr2 = [S(0), S(1), S(2), S(3)];
104-
arr1 = _d_arrayctor!(S[4])(arr2[], arr1.length);
98+
arr1 = _d_arrayctor(arr2[], arr1.length);
10599

106100
assert(counter == 4);
107101
assert(arr1 == arr2);
@@ -124,7 +118,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
124118

125119
S[4] arr1;
126120
S[4] arr2 = [S(0), S(1), S(2), S(3)];
127-
arr1 = _d_arrayctor!(S[4])(arr2[], arr1.length);
121+
arr1 = _d_arrayctor(arr2[], arr1.length);
128122

129123
assert(counter == 4);
130124
assert(arr1 == arr2);
@@ -150,7 +144,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
150144
{
151145
Throw[4] a;
152146
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
153-
a = _d_arrayctor!(Throw[4])(b[], a.length);
147+
a = _d_arrayctor(b[], a.length);
154148
}
155149
catch (Exception)
156150
{
@@ -175,7 +169,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
175169
{
176170
NoThrow[4] a;
177171
NoThrow[4] b = [NoThrow(1), NoThrow(2), NoThrow(3), NoThrow(4)];
178-
a = _d_arrayctor!(NoThrow[4])(b[], a.length);
172+
a = _d_arrayctor(b[], a.length);
179173
}
180174
catch (Exception)
181175
{
@@ -199,6 +193,7 @@ Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t
199193
void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
200194
{
201195
pragma(inline, false);
196+
import core.internal.traits : Unqual;
202197
import core.lifetime : copyEmplace;
203198

204199
size_t i;
@@ -281,7 +276,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
281276
{
282277
Throw[4] a;
283278
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
284-
a = _d_arrayctor!(Throw[4])(b[], a.length);
279+
a = _d_arrayctor(b[], a.length);
285280
}
286281
catch (Exception)
287282
{

0 commit comments

Comments
 (0)