D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7477 - Enum structs without specified values
Summary: Enum structs without specified values
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2012-02-09 14:00 UTC by Don
Modified: 2014-08-10 12:54 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2012-02-09 14:00:35 UTC
Bug 4423 considers the case of enum structs where all values are explicitly specified; it requires a particular opCmp signature.

The case of enum structs when values are NOT specified is even more problematic.

struct Foo { int x; }
enum Bar : Foo { a, b }

For this to work, it is required that Foo supports:
* implicit cast from 0 to Foo.
* a .max property
* + 1
* and opCmp of the correct signature.

I think this is a useless feature, and should simply be dropped.

Currently, the test case gives some unhelpful error messages, containing __error and no line number:


crash.d(3): Error: cannot implicitly convert expression (0) of type int to Foo66
99
Error: no property 'max' for type 'Foo6699'
crash.d(3): Error: Integer constant expression expected instead of (__error) == (__error)
Comment 1 Jonathan M Davis 2012-02-09 14:16:33 UTC
The same thing happens if you do something like

enum Foo : string { a, b }

q.d(3): Error: cannot implicitly convert expression (0) of type int to string
Error: no property 'max' for type 'string'
q.d(3): Error: Integer constant expression expected instead of (__error) == (__error)

So, it's not really struct-specific. It affects all types which can't be implicitly assigned 0.

It would probably be simplest to require that enums must either be values or they must be primitive types other than arrays, or maybe outright require that they be integral values (I question the usefulness of using bool or floating point enums without specifying their values).
Comment 2 Kenji Hara 2014-08-10 10:13:01 UTC
In git-head, diagnostic has been improved.

(In reply to Don from comment #0)
> struct Foo { int x; }
> enum Bar : Foo { a, b }

test.d(2): Error: cannot implicitly convert expression (0) of type int to Foo

(In reply to Jonathan M Davis from comment #1)
> The same thing happens if you do something like
> 
> enum Foo : string { a, b }

test.d(1): Error: cannot implicitly convert expression (0) of type int to string
Comment 3 github-bugzilla 2014-08-10 12:54:40 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/4cf455e6c5d686aa239300f3f6e955f6e8ceac66
fix Issue 7477 - Enum structs without specified values