D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22644 - Using @disable on enum members produces unrelated errors
Summary: Using @disable on enum members produces unrelated errors
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-01-02 09:09 UTC by Tomer Filiba (weka)
Modified: 2022-01-03 05:11 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomer Filiba (weka) 2022-01-02 09:09:28 UTC
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 moonlightsentinel 2022-01-02 18:23:57 UTC
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 Tejas_Garhewal 2022-01-03 05:11:44 UTC
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