Bug 14501 – template result is reused for equal but non-identical alias arguments

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-04-25T20:21:09Z
Last change time
2024-12-13T18:42:31Z
Keywords
wrong-code
Assigned to
No Owner
Creator
ag0aep6g
Blocks
10190
See also
https://issues.dlang.org/show_bug.cgi?id=22540
Moved to GitHub: dmd#18982 →

Comments

Comment #0 by ag0aep6g — 2015-04-25T20:21:09Z
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
Using T... intead of an alias worked until https://github.com/dlang/dmd/pull/1368
Comment #6 by robert.schadek — 2024-12-13T18:42:31Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18982 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB