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

Commit 3e233e7

Browse files
committed
Fix Issue 19008 - core.internal.convert.toUbyte doesn't work on enums
1 parent 6f750d8 commit 3e233e7

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/core/internal/convert.d

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,20 +549,28 @@ const(ubyte)[] toUbyte(T)(ref T val) if (is(Unqual!T == cfloat) || is(Unqual!T =
549549
}
550550

551551
@trusted pure nothrow
552-
const(ubyte)[] toUbyte(T)(ref T val) if (is(T == enum) && is(typeof(toUbyte(cast(V)val)) == const(ubyte)[]))
552+
const(ubyte)[] toUbyte(T)(ref T val) if (is(T V == enum) && is(typeof(toUbyte(cast(V)val)) == const(ubyte)[]))
553553
{
554554
if (__ctfe)
555555
{
556556
static if (is(T V == enum)){}
557-
V e_val = val;
558-
return toUbyte(e_val);
557+
return toUbyte(cast(V) val);
559558
}
560559
else
561560
{
562561
return (cast(const(ubyte)*)&val)[0 .. T.sizeof];
563562
}
564563
}
565564

565+
nothrow pure @safe unittest
566+
{
567+
// Issue 19008 - check toUbyte works on enums.
568+
enum Month : uint { jan = 1}
569+
Month m = Month.jan;
570+
const bytes = toUbyte(m);
571+
enum ctfe_works = (() => { Month x = Month.jan; return toUbyte(x).length > 0; })();
572+
}
573+
566574
private bool isNonReference(T)()
567575
{
568576
static if (is(T == struct) || is(T == union))

0 commit comments

Comments
 (0)