Skip to content

Commit f52a44d

Browse files
Fix Bugzilla Issue 24818 Sumtype with single-type wastes space (#10642)
* Fix Bugzilla Issue 24818 Sumtype with single-type wastes space * Move unittest to correct position * Annotate unittest with @safe
1 parent d4dd391 commit f52a44d

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

std/sumtype.d

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ private:
304304
}
305305

306306
Storage storage;
307-
Tag tag;
307+
static if (Types.length > 1)
308+
Tag tag;
309+
else
310+
enum Tag tag = 0;
308311

309312
/* Accesses the value stored in a SumType by its index.
310313
*
@@ -369,7 +372,8 @@ public:
369372
storage.tupleof[tid] = forward!value;
370373
}
371374

372-
tag = tid;
375+
static if (Types.length > 1)
376+
tag = tid;
373377
}
374378

375379
static if (isCopyable!(const(T)))
@@ -380,7 +384,8 @@ public:
380384
this(const(T) value) const
381385
{
382386
storage.tupleof[tid] = value;
383-
tag = tid;
387+
static if (Types.length > 1)
388+
tag = tid;
384389
}
385390
}
386391
}
@@ -397,7 +402,8 @@ public:
397402
this(immutable(T) value) immutable
398403
{
399404
storage.tupleof[tid] = value;
400-
tag = tid;
405+
static if (Types.length > 1)
406+
tag = tid;
401407
}
402408
}
403409
}
@@ -415,7 +421,8 @@ public:
415421
if (is(Value == DeducedParameterType!(inout(T))))
416422
{
417423
storage.tupleof[tid] = value;
418-
tag = tid;
424+
static if (Types.length > 1)
425+
tag = tid;
419426
}
420427
}
421428
}
@@ -449,7 +456,8 @@ public:
449456
return newStorage;
450457
});
451458

452-
tag = other.tag;
459+
static if (Types.length > 1)
460+
tag = other.tag;
453461
}
454462
}
455463
else
@@ -469,7 +477,8 @@ public:
469477
return newStorage;
470478
});
471479

472-
tag = other.tag;
480+
static if (Types.length > 1)
481+
tag = other.tag;
473482
}
474483
}
475484
else
@@ -493,7 +502,8 @@ public:
493502
return newStorage;
494503
});
495504

496-
tag = other.tag;
505+
static if (Types.length > 1)
506+
tag = other.tag;
497507
}
498508
}
499509
else
@@ -517,7 +527,8 @@ public:
517527
return newStorage;
518528
});
519529

520-
tag = other.tag;
530+
static if (Types.length > 1)
531+
tag = other.tag;
521532
}
522533
}
523534
else
@@ -644,7 +655,8 @@ public:
644655
}
645656

646657
storage = newStorage;
647-
tag = tid;
658+
static if (Types.length > 1)
659+
tag = tid;
648660

649661
return this;
650662
}
@@ -1558,6 +1570,13 @@ version (D_BetterC) {} else
15581570
enum result = test();
15591571
}
15601572

1573+
// https://github.com/dlang/phobos/issues/10563
1574+
// Do not waste space for tag if sumtype has only single type
1575+
@safe unittest
1576+
{
1577+
static assert(SumType!int.sizeof == int.sizeof);
1578+
}
1579+
15611580
/// True if `T` is an instance of the `SumType` template, otherwise false.
15621581
private enum bool isSumTypeInstance(T) = is(T == SumType!Args, Args...);
15631582

0 commit comments

Comments
 (0)