www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11101] New: Invalid enum member overflow message

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

           Summary: Invalid enum member overflow message
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bugzilla digitalmars.com


--- Comment #0 from Walter Bright <bugzilla digitalmars.com> 2013-09-22
10:43:17 PDT ---
public enum GTokenType
{
    NONE,
}

public enum GtkRcTokenType
{
    INVALID = GTokenType.NONE,
    INCLUDE,
}

causes:

test.d(9): Error: enum member test.GtkRcTokenType.INCLUDE initialization with
(cast(GtkRcTokenType)cast(GTokenType)0 + 1) causes overflow

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


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru


--- Comment #1 from Maxim Fomin <maxim maxim-fomin.ru> 2013-09-22 10:54:27 PDT
---
Citing spec: "If the EnumBaseType is not explicitly set, and the first
EnumMember has an initializer, it is set to the type of that initializer.
Otherwise, it defaults to type int." 

Since second enum is not explicitly based on int and has first enum
initializer, that's why type of second enum is deduced to be GTokenType and
overflow occures as the first enum has single member. So, according to current
spec this is RESOLVED-INVALID. 

On the other hand, nothing stops from allowing such code for facilitation
purposes by fixing spec.

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



--- Comment #2 from Mike Wey <mike-wey planet.nl> 2013-09-22 13:11:20 PDT ---
That would be a valid explanation only the following compiles successfully:

public enum GTokenType
{
    EOF = 0,
    LEFT_PAREN = '(',
    RIGHT_PAREN = ')',
    LEFT_CURLY = '{',
    RIGHT_CURLY = '}',
    LEFT_BRACE = '[',
    RIGHT_BRACE = ']',
    EQUAL_SIGN = '=',
    COMMA = ',',
    NONE = 110,
    ERROR,
    CHAR,
    BINARY,
    OCTAL,
    INT,
    HEX,
    FLOAT,
    STRING,
    SYMBOL,
    IDENTIFIER,
    IDENTIFIER_NULL,
    COMMENT_SINGLE,
    COMMENT_MULTI,
    LAST
}

public enum GtkRcTokenType
{
    INVALID = GTokenType.LAST,
    INCLUDE,
}

If the value of NONE is larger than 110 it fails to compile.

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #3 from Walter Bright <bugzilla digitalmars.com> 2013-09-22
17:20:11 PDT ---
It should fail to compile with NONE larger than 110.

The algorithm is if GTokenType.LAST==GTokenType.max, then attempting to
calculate GTokenType.LAST+1 overflows it. To get what you are looking for,
use:

enum GtkRcTokenType
{
    INVALID = GTokenType.LAST + 1,
    INCLUDE,
}

which will compile successfully.

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



--- Comment #4 from Mike Wey <mike-wey planet.nl> 2013-09-23 10:02:39 PDT ---
(In reply to comment #3)
 It should fail to compile with NONE larger than 110.
 
 The algorithm is if GTokenType.LAST==GTokenType.max, then attempting to
 calculate GTokenType.LAST+1 overflows it.
110 succeeds 111 fails in both cases GTokenType.LAST==GTokenType.max, both cases should either fail or succeed. Also note that when part of gtkD the enum overflows when NONE is 87, as far as i can tell this depends on the size of the GtkRcTokenType enum. This seems a bit inconsistent.
 To get what you are looking for, use:
 
 enum GtkRcTokenType
 {
     INVALID = GTokenType.LAST + 1,
     INCLUDE,
 }
 
 which will compile successfully.
I'm casting GTokenType.LAST to int because the values of the enum need to correspond with there C counter part. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 23 2013