From 52462bec6ea846f30e8dac309d63ccb4101e8f0c Mon Sep 17 00:00:00 2001 From: k-hara Date: Sun, 15 Apr 2012 10:55:21 +0900 Subject: [PATCH] fix Issue 7909 - to!(enum)(string) and to!(string)(enum) break when enum is integral --- changelog.dd | 1 + std/conv.d | 26 ++++++++++++++------------ std/traits.d | 29 ++++++++++++++++------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/changelog.dd b/changelog.dd index 33c37f874..0f7f1999b 100644 --- a/changelog.dd +++ b/changelog.dd @@ -6,6 +6,7 @@ $(VERSION 060, ddd mm, 2012, =================================================, ) $(LIBBUGSFIXED + $(LI $(BUGZILLA 7909): to!enum(string) and to!string(enum) break when enum is integral) ) ) $(VERSION 059, ddd mm, 2012, =================================================, diff --git a/std/conv.d b/std/conv.d index 34a8a2c3e..2969e4aa0 100644 --- a/std/conv.d +++ b/std/conv.d @@ -1048,21 +1048,23 @@ T toImpl(T, S)(S s) unittest { debug(conv) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " succeeded."); - enum E { a, b, c } - assert(to! string(E.a) == "a"c); - assert(to!wstring(E.b) == "b"w); - assert(to!dstring(E.c) == "c"d); - enum F : real { x = 1.414, y = 1.732, z = 2.236 } - assert(to! string(F.x) == "x"c); - assert(to!wstring(F.y) == "y"w); - assert(to!dstring(F.z) == "z"d); + enum EU { a = 0, b = 1, c = 2 } // base type is unsigned + enum EI { a = -1, b = 0, c = 1 } // base type is signed (bug 7909) + enum EF : real { a = 1.414, b = 1.732, c = 2.236 } + + foreach (E; TypeTuple!(EU, EI, EF)) + { + assert(to! string(E.a) == "a"c); + assert(to!wstring(E.a) == "a"w); + assert(to!dstring(E.a) == "a"d); + } // Test an value not corresponding to an enum member. - auto o = cast(E)5; - assert(to! string(o) == "cast(E)5"c); - assert(to!wstring(o) == "cast(E)5"w); - assert(to!dstring(o) == "cast(E)5"d); + auto o = cast(EU)5; + assert(to! string(o) == "cast(EU)5"c); + assert(to!wstring(o) == "cast(EU)5"w); + assert(to!dstring(o) == "cast(EU)5"d); } /// ditto diff --git a/std/traits.d b/std/traits.d index 7851a18c2..9250b61c7 100644 --- a/std/traits.d +++ b/std/traits.d @@ -2700,7 +2700,7 @@ unittest /* */ -template BooleanTypeOf(T) +template BooleanTypeOf(T) if (!is(T == enum)) { inout(bool) idx( inout(bool) ); shared(inout bool) idx( shared(inout bool) ); @@ -2734,7 +2734,7 @@ unittest /* */ -template IntegralTypeOf(T) +template IntegralTypeOf(T) if (!is(T == enum)) { inout( byte) idx( inout( byte) ); inout( ubyte) idx( inout( ubyte) ); @@ -2793,7 +2793,7 @@ unittest /* */ -template FloatingPointTypeOf(T) +template FloatingPointTypeOf(T) if (!is(T == enum)) { inout( float) idx( inout( float) ); inout(double) idx( inout(double) ); @@ -2832,7 +2832,7 @@ unittest /* */ -template NumericTypeOf(T) +template NumericTypeOf(T) if (!is(T == enum)) { static if (is(IntegralTypeOf!T X)) alias X NumericTypeOf; @@ -2860,7 +2860,7 @@ unittest /* */ -template UnsignedTypeOf(T) +template UnsignedTypeOf(T) if (!is(T == enum)) { static if (is(IntegralTypeOf!T X) && staticIndexOf!(Unqual!X, UnsignedIntTypeList) >= 0) @@ -2869,7 +2869,7 @@ template UnsignedTypeOf(T) static assert(0, T.stringof~" is not an unsigned type."); } -template SignedTypeOf(T) +template SignedTypeOf(T) if (!is(T == enum)) { static if (is(IntegralTypeOf!T X) && staticIndexOf!(Unqual!X, SignedIntTypeList) >= 0) @@ -2882,7 +2882,7 @@ template SignedTypeOf(T) /* */ -template CharTypeOf(T) +template CharTypeOf(T) if (!is(T == enum)) { inout( char) idx( inout( char) ); inout(wchar) idx( inout(wchar) ); @@ -2937,7 +2937,7 @@ unittest /* */ -template StaticArrayTypeOf(T) +template StaticArrayTypeOf(T) if (!is(T == enum)) { inout(U[n]) idx(U, size_t n)( inout(U[n]) ); @@ -2968,7 +2968,7 @@ unittest /* */ -template DynamicArrayTypeOf(T) +template DynamicArrayTypeOf(T) if (!is(T == enum)) { inout(U[]) idx(U)( inout(U[]) ); @@ -3008,7 +3008,7 @@ unittest /* */ -template ArrayTypeOf(T) +template ArrayTypeOf(T) if (!is(T == enum)) { static if (is(StaticArrayTypeOf!T X)) alias X ArrayTypeOf; @@ -3024,7 +3024,7 @@ unittest /* */ -template StringTypeOf(T) if (isSomeString!T) +template StringTypeOf(T) if (!is(T == enum) && isSomeString!T) { alias ArrayTypeOf!T StringTypeOf; } @@ -3054,7 +3054,7 @@ unittest /* */ -template AssocArrayTypeOf(T) +template AssocArrayTypeOf(T) if (!is(T == enum)) { immutable(V [K]) idx(K, V)( immutable(V [K]) ); @@ -3286,7 +3286,10 @@ Detect whether we can treat T as one of the built-in string types. */ template isSomeString(T) { - enum isSomeString = isNarrowString!T || is(T : const(dchar[])); + static if (is(T == enum)) + enum isSomeString = false; + else + enum isSomeString = isNarrowString!T || is(T : const(dchar[])); } unittest