Bug 22644 – Using @disable on enum members produces unrelated errors
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-01-02T09:09:28Z
Last change time
2022-01-03T05:11:44Z
Assigned to
No Owner
Creator
Tomer Filiba (weka)
Comments
Comment #0 by tomer — 2022-01-02T09:09:28Z
Consider the following snippet:
----------------------------------
enum Foo {
x = 7,
@disable y = 8,
}
void main() {
writefln("Hello %s", Foo.x);
}
----------------------------------
Trying to run this, produces this error:
----------------------------------
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d(4149): Error: enum member `onlineapp.Foo.y` cannot be used because it is annotated with `@disable`
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d-mixin-4137(4137): Error: enum member `onlineapp.Foo.y` cannot be used because it is annotated with `@disable`
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d(4149): Error: template instance `std.traits.EnumMembers!(Foo).WithIdentifier!"y".Symbolize!(Foo.y)` error instantiating
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d(4158): instantiated from here: `EnumSpecificMembers!"y"`
/dlang/dmd/linux/bin64/../../src/phobos/std/traits.d(4167): instantiated from here: `EnumSpecificMembers!("x", "y")`
/dlang/dmd/linux/bin64/../../src/phobos/std/format/internal/write.d(2857): instantiated from here: `EnumMembers!(Foo)`
/dlang/dmd/linux/bin64/../../src/phobos/std/format/write.d(1239): ... (3 instantiations, -v to show) ...
/dlang/dmd/linux/bin64/../../src/phobos/std/stdio.d(4449): instantiated from here: `writefln!(char, Foo)`
onlineapp.d(12): instantiated from here: `writefln!(char, Foo)`
----------------------------------
Because generating the string representation of the enum uses EnumMembers, which "refers" to `y` even though my code snippet doesn't use `y` at all
Comment #1 by moonlightsentinel — 2022-01-02T18:23:57Z
The template instance for `writefln` has no knowledge about the value passed in the provided snipped and hence has to accommodate for each possible value of `Foo`.
So at most this is an enhancement for Phobos to skip disabled enum members.
Comment #2 by scienticman — 2022-01-03T05:11:44Z
I understand that this issue has been declared resolved, but a workaround until the underlying issue is solved(via enhancing phobos or something else) is to use stringof property
```d
enum Foo {
x = 7,
@disable y = 8,
}
void main() {
writefln("Hello %s", Foo.x.stringof);
}
```
It also correctly fails to compile if you write Foo.y.stringof