Thread overview
[Issue 14950] Setting enum value to the last member of another enum causes int overflow error
Aug 23, 2015
ag0aep6g@gmail.com
Sep 01, 2015
Kenji Hara
Sep 01, 2015
Kenji Hara
August 23, 2015
https://issues.dlang.org/show_bug.cgi?id=14950

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
                 CC|                            |ag0aep6g@gmail.com

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14950

Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |diagnostic
           Hardware|x86_64                      |All
                 OS|Linux                       |All
           Severity|normal                      |minor

--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> ---
This is a minor diagnostic issue.

In D, a derived enum type cannot overflow the value range of its base type.

http://dlang.org/enum

> If there is no AssignExpression and it is not the first EnumMember, it is given the value of the previous EnumMember+1. If the value of the previous EnumMember is EnumBaseType.max, it is an error. If the value of the previous EnumMember+1 is the same as the value of the previous EnumMember, it is an error. (This can happen with floating point types.)

With the definition of A and B

> 	enum A {
> 		start,
> 		end
> 	}
> 
> 	enum B {
> 		start = A.end,
> 		end
> 	}

The base type of B is implicitly deduced to A, because B.start is defined by using A.end.

So B.end is beyond the value of A.max (== A.end), and compiler reports an
overflow error.

But current error message is a little confusing. The current message

> ./test.d(9): Error: enum member test.B.end initialization with (B.start + 1) causes overflow for type 'int'

should become:

  ./test.d(9): Error: enum member test.B.end initialization with (B.start + 1)
causes overflow for type 'A'

----

To avoid the error, you need to specify the base of each enum types explicitly. You're intended that the two enums are for C library interfaces, so the best for that would be int.

A fixup example:

    enum A : int {
        start,   // == int(0)
        end      // == int(1)
    }

    enum B : int {
        start = A.end,  // == cast(int)A.end == int(0)
        end             // == int(1)
    }

Then, the B definition never overflows from its base type range: 0 to int.max.

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14950

Kenji Hara <k.hara.pg@gmail.com> changed:

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

--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> ---
https://github.com/D-Programming-Language/dmd/pull/5006

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14950

github-bugzilla@puremagic.com changed:

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

--
September 01, 2015
https://issues.dlang.org/show_bug.cgi?id=14950

--- Comment #3 from github-bugzilla@puremagic.com ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/061ac36a75866ec6cb6827c1bb99263764ccac14
fix Issue 14950 - Setting enum value to the last member of another enum causes
int overflow error

https://github.com/D-Programming-Language/dmd/commit/2748404b7ad3ba8e294d3c031bbd138942f56a79 Merge pull request #5006 from 9rnsr/fix14950

Issue 14950 - Setting enum value to the last member of another enum causes int overflow error

--