Skip to content

Commit 875015e

Browse files
committed
Fix Issue 10879 - std.variant Variant/Algebraic: Cant store static arrays > 32(/16) bytes
1 parent b78e7b3 commit 875015e

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

std/variant.d

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,17 @@ private:
339339
static if (target.size < A.sizeof)
340340
{
341341
if (target.type.tsize < A.sizeof)
342-
*cast(A**)&target.store = new A;
342+
{
343+
static if (is(A == U[n], U, size_t n))
344+
{
345+
A* p = cast(A*)(new U[n]).ptr;
346+
}
347+
else
348+
{
349+
A* p = new A;
350+
}
351+
*cast(A**)&target.store = p;
352+
}
343353
}
344354
tryPutting(zis, typeid(A), cast(void*) getPtr(&target.store))
345355
|| assert(false);
@@ -635,6 +645,11 @@ public:
635645
{
636646
auto p = new T(rhs);
637647
}
648+
else static if (is(T == U[n], U, size_t n))
649+
{
650+
auto p = cast(T*)(new U[n]).ptr;
651+
*p = rhs;
652+
}
638653
else
639654
{
640655
auto p = new T;
@@ -1245,6 +1260,32 @@ public:
12451260
assertThrown!VariantException(v[1] = Variant(null));
12461261
}
12471262

1263+
//Issue# 10879
1264+
@system unittest
1265+
{
1266+
int[10] arr = [1,2,3,4,5,6,7,8,9,10];
1267+
Variant v1 = arr;
1268+
Variant v2;
1269+
v2 = arr;
1270+
assert(v1 == arr);
1271+
assert(v2 == arr);
1272+
foreach (i, e; arr)
1273+
{
1274+
assert(v1[i] == e);
1275+
assert(v2[i] == e);
1276+
}
1277+
static struct LargeStruct
1278+
{
1279+
int[100] data;
1280+
}
1281+
LargeStruct ls;
1282+
ls.data[] = 4;
1283+
v1 = ls;
1284+
Variant v3 = ls;
1285+
assert(v1 == ls);
1286+
assert(v3 == ls);
1287+
}
1288+
12481289
//Issue# 8195
12491290
@system unittest
12501291
{

0 commit comments

Comments
 (0)