Bug 8718 – Template mixin + string mixin name collision
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-09-23T23:47:00Z
Last change time
2014-07-26T15:14:45Z
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2012-09-23T23:47:48Z
From the (simplified) discussion @ http://forum.dlang.org/thread/[email protected]
----
mixin template T(string s)
{
void f()
{
mixin("++" ~ s ~ ";"); //L5
}
}
struct S
{
int x;
}
void main() {
S a;
S s;
mixin T!("a.x");
mixin T!("s.x"); //L18
}
----
main.d(5): Error: undefined identifier 'x', did you mean 'struct S'?
main.d(18): Error: mixin main.main.T!("s.x") error instantiating
----
When trying to instantiate the template mixin T the second time with "s.x", the compiler gets confused because the name of the string parameter is also "s", making "s.x" illegal.
I think this is a catch 22:
- On the one hand, the code generated by the template is, AFAIK, genuinely illegal, as s shadows the outside world's s, and the generated "s.x" is indeed illegal.
- On the other hand, I think it is not conceivable to tell a client "your code failed to work, because you declared your variable with the same name as the argument of a function: You are not allowed to call your variable s because it's taken." WHAT?
Not sure what to make of this...
Comment #1 by yebblies — 2013-11-24T05:26:14Z
I don't think this is a bug... If you use mixins, you may get symbol conflicts.
Comment #2 by yebblies — 2014-07-26T15:14:45Z
I think this is working correctly, with the template parameter shadowing the local variable. It is fairly easy to work around by either renaming the variable or using alias parameters instead of string mixins.