Bug 16125 – mixin string/template confusion, results in no-op
Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2016-06-05T11:07:00Z
Last change time
2016-06-05T11:39:39Z
Assigned to
nobody
Creator
tomer
Comments
Comment #0 by tomer — 2016-06-05T11:07:12Z
consider the following:
---------
enum Stub(string s) = s;
mixin Stub!"int x;";
void main() {
writeln(x); // error, x is undefined
}
--------
Stub!S obviously does different things under different conditions, but it doesn't matter. it seems that dmd is confused by the the *syntax*, which looks like mixin templates, while it's actually a string. so
mixin "int x;";
is illegal in the syntax, but my code passes parsing, but simply doesn't do anything. what i should have used is:
mixin(Foo!"int x;");
which works, but the previous version must either do the same or be a compiler error.
Comment #1 by ag0aep6g — 2016-06-05T11:39:39Z
(In reply to Tomer Filiba from comment #0)
> enum Stub(string s) = s;
This expands to `template Stub(string s) { enum Stub = s; }
> mixin Stub!"int x;";
This mixes in `enum Stub s = "int x;";`. The mixed in `Stub` is shadowed by the template of the same name. We can give the mixin a name to go around that:
mixin Stub!"int x;" MixedInStub;
Then we can print it:
void main() { writeln(MixedInStub.Stub); } /* prints "int x;" */
The problem in all this is that dmd allows to mixin a template that's not a `mixin template`. Issue 12298 calls this out.
The behavior you're asking for (either behave as string mixin, or fail to compile) is also being asked for in issue 16025.
Closing this as a duplicate of 16025.
*** This issue has been marked as a duplicate of issue 16025 ***