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

Commit c259006

Browse files
committed
Add draft refactor for _d_arrayctor
This aims to solve the warnings issued by the compiler when lowering to strongly pure calls such as `_d_arrayctor(a, b)` and ignoring the return value. Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
1 parent 077db71 commit c259006

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/core/internal/array/construction.d

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
module 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:
@@ -21,7 +23,8 @@ module core.internal.array.construction;
2123
* purity, and throwabilty checks. To prevent breaking existing code, this function template
2224
* is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
2325
*/
24-
Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
26+
Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from, size_t length) @trusted
27+
if (is(Unqual!T1 == Unqual!T2))
2528
{
2629
pragma(inline, false);
2730
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
@@ -30,7 +33,15 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
3033
import core.stdc.stdint : uintptr_t;
3134
debug(PRINTF) import core.stdc.stdio : printf;
3235

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);
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);
37+
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+
}
3445

3546
void[] vFrom = (cast(void*)from.ptr)[0..from.length];
3647
void[] vTo = (cast(void*)to.ptr)[0..to.length];
@@ -46,13 +57,11 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
4657
(cast(Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false);
4758
}
4859

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

51-
static if (hasElaborateCopyConstructor!T)
62+
static if (hasElaborateCopyConstructor!T1)
5263
{
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;
64+
size_t i;
5665
try
5766
{
5867
for (i = 0; i < to.length; i++)
@@ -64,7 +73,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
6473
*/
6574
while (i--)
6675
{
67-
auto elem = cast(Unqual!T*)&to[i];
76+
auto elem = cast(Unqual!T1*)&to[i];
6877
destroy(*elem);
6978
}
7079

@@ -74,7 +83,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
7483
else
7584
{
7685
// blit all elements at once
77-
memcpy(cast(void*) to.ptr, from.ptr, to.length * T.sizeof);
86+
memcpy(cast(void*) to.ptr, from.ptr, to.length * T1.sizeof);
7887
}
7988

8089
return to;
@@ -92,7 +101,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
92101

93102
S[4] arr1;
94103
S[4] arr2 = [S(0), S(1), S(2), S(3)];
95-
_d_arrayctor(arr1[], arr2[]);
104+
arr1 = _d_arrayctor!(S[4])(arr2[], arr1.length);
96105

97106
assert(counter == 4);
98107
assert(arr1 == arr2);
@@ -115,7 +124,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
115124

116125
S[4] arr1;
117126
S[4] arr2 = [S(0), S(1), S(2), S(3)];
118-
_d_arrayctor(arr1[], arr2[]);
127+
arr1 = _d_arrayctor!(S[4])(arr2[], arr1.length);
119128

120129
assert(counter == 4);
121130
assert(arr1 == arr2);
@@ -141,7 +150,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
141150
{
142151
Throw[4] a;
143152
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
144-
_d_arrayctor(a[], b[]);
153+
a = _d_arrayctor!(Throw[4])(b[], a.length);
145154
}
146155
catch (Exception)
147156
{
@@ -166,7 +175,7 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
166175
{
167176
NoThrow[4] a;
168177
NoThrow[4] b = [NoThrow(1), NoThrow(2), NoThrow(3), NoThrow(4)];
169-
_d_arrayctor(a[], b[]);
178+
a = _d_arrayctor!(NoThrow[4])(b[], a.length);
170179
}
171180
catch (Exception)
172181
{
@@ -190,7 +199,6 @@ Tarr _d_arrayctor(Tarr : T[], T)(return scope Tarr to, scope Tarr from) @trusted
190199
void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
191200
{
192201
pragma(inline, false);
193-
import core.internal.traits : Unqual;
194202
import core.lifetime : copyEmplace;
195203

196204
size_t i;
@@ -273,7 +281,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
273281
{
274282
Throw[4] a;
275283
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
276-
_d_arrayctor(a[], b[]);
284+
a = _d_arrayctor!(Throw[4])(b[], a.length);
277285
}
278286
catch (Exception)
279287
{

0 commit comments

Comments
 (0)