www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6893] New: Write of enum member represented with ubyte or ulong

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893

           Summary: Write of enum member represented with ubyte or ulong
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.stdio;
enum E1 : uint  { A, B, C };
enum E2 : ubyte { A, B, C };
enum E3 : ulong { A, B, C };
void main() {
    writeln(E1.C);
    writeln(E2.C);
    writeln(E3.C);
}


Output dmd 2.057head:

C

<error>


Expected output:

C
C
C

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 05 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



10:35:38 PST ---
First two are ok now but the 3rd has become a massive template instance error
(2.061).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




The problem appears to be std.format.getNthInt, under "static if
(isIntegral!...)": the code assumes that if a type is integral, it can be
converted to int, but this is not true when the base type of the enum is ulong.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




Hmm actually, there appears to be a bug somewhere else. The spec shouldn't be
spec.DYNAMIC at all, since the default spec for enums is "%s". So something
went wrong before this point.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




17:33:31 PDT ---

 Hmm actually, there appears to be a bug somewhere else. The spec shouldn't be
 spec.DYNAMIC at all, since the default spec for enums is "%s". So something
 went wrong before this point.
Last time I tried to fix this I lost my head in the formatting jungle. Good luck! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




Aha! I found the reason: getNthInt is called from a *compile-time* check, so
even if the spec for enum is "%s", the compiler still has to compile this
branch of the code. So the fix is for getNthInt to *statically* verify
convertibility to int before attempting to call to!int.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




Argh. I wish bugzilla allowed editing of comments... What I meant was:
getNthInt is called from inside a *runtime* if-condition that evaluates to
false when the spec is "%s" and the argument is a ulong enum, but since this
if-condition can only be checked at runtime, getNthInt must be able to compile
in that case. Currently it doesn't compile because it assumes isIntegral!T
means T can be implicitly converted to int, which is not true if the argument
is a ulong (or a ulong enum). So the fix is to replace isIntegral with
is(typeof(to!int(...))) so that at compile-time the compiler will hardcode the
function to throw an exception when an invalid argument is given. (It won't
actually throw at runtime if the if-condition in the caller evaluates to false
at runtime.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



https://github.com/D-Programming-Language/phobos/pull/1504

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/b680614e571c52b45cef9288e15ec3df601828a3
Add unittest for issue 6893.

https://github.com/D-Programming-Language/phobos/commit/cbc684a001e53e3f1fc95ef6caddb0c66095e2f0


Issue 6893 - Write of enum member represented with ubyte or ulong

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 24 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6893


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |monarchdodra gmail.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 24 2013