Bug 23522 – Error message when enum type is not integral and a value lacks an initializer
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-11-30T14:37:42Z
Last change time
2023-09-08T18:19:18Z
Keywords
bootcamp, diagnostic, pull
Assigned to
No Owner
Creator
Bolpat
Comments
Comment #0 by qs.il.paperinik — 2022-11-30T14:37:42Z
If an `enum` has a type that is not numerical, every value must have an initializer:
```d
enum X : string
{
a = "a",
b // error
}
```
The error message should be very specific (“`b` has no initializer and the underyling type is not integral”), but it is:
```
Error: no property `max` for type `string`, perhaps `import std.algorithm;` is needed?
Error: incompatible types for `("a") + (1)`: `X` and `int`
```
Comment #1 by b2.temp — 2023-01-02T12:17:14Z
semantics are more subtle, the increment from the previous value can work for non-integral types too:
```
struct FooInt
{
int i;
auto opBinary(string op : "+")(int j)
{
return typeof(this)(i + j);
}
static @property FooInt max()
{
return typeof(this)(int.max);
}
}
enum foolist
{
hi = FooInt(0),
bye
}
```
So you can only determine if the implicit value can be set by trying it.
Comment #2 by dlang-bot — 2023-09-08T16:30:05Z
@ntrel created dlang/dmd pull request #15588 "Improve error messages when enum member cannot be generated" fixing this issue:
- Fix Issue 23522 - Error message when enum type is not integral and a value lacks an initializer
https://github.com/dlang/dmd/pull/15588
Comment #3 by nick — 2023-09-08T16:49:53Z
With the PR, dmd will give introductory errors which should help the others make sense:
enumstr.d(4): Error: cannot check `enumstr.X.b` value for overflow
enumstr.d(1): Error: no property `max` for type `string`, perhaps `import std.algorithm;` is needed?
enumstr.d(4): Error: cannot generate value for `enumstr.X.b`
enumstr.d(4): Error: incompatible types for `("a") + (1)`: `X` and `int`
Comment #4 by dlang-bot — 2023-09-08T18:19:18Z
dlang/dmd pull request #15588 "Improve error messages when enum member cannot be generated" was merged into master:
- fecd435f695f030ac4d1de548bb00e4f690dfacd by Nick Treleaven:
Fix Issue 23522 - Error message when enum type is not integral and a value lacks an initializer
https://github.com/dlang/dmd/pull/15588