Spin-off from issue 10190.
----
enum a = 1;
enum b = 1;
static assert(__traits(identifier, a) == "a"); /* passes */
static assert(__traits(identifier, b) == "b"); /* passes */
template identifier(alias symbol)
{
enum identifier = __traits(identifier, symbol);
}
pragma(msg, identifier!a); /* prints "a", ok */
pragma(msg, identifier!b); /* prints "a" too, should print "b" */
----
When printing both identifier!a and identifier!b with pragma(msg,...) it shows that they're both "a". When the order is switched, they both become "b". Apparently, the value is reused when it shouldn't be.
Comment #1 by razvan.nitu1305 — 2017-07-27T11:10:37Z
The problem here is that when the template is instantiated the compiler basically generates, roughly, the following code:
auto identifier(enum a)
{
return __traits(identifier, a);
}
Subsequent calls to identifier which do not instantiate the template will just use the already generated function (I'm not sure if a function is generated, but that is not important here) thus making traits evaluate the identifier to the first identifier that was used to instantiate the template.
Comment #2 by ag0aep6g — 2018-08-20T12:28:12Z
*** Issue 18569 has been marked as a duplicate of this issue. ***
Comment #3 by bugzilla — 2020-01-11T08:29:34Z
*** Issue 17441 has been marked as a duplicate of this issue. ***
Comment #4 by bugzilla — 2020-01-11T08:31:59Z
Raising importance to "major", because this causes several issues in Phobos, where no workaround exists.
Comment #5 by moonlightsentinel — 2020-02-15T13:14:32Z