Apologies for the vague title, but I'm not quite sure how to appropriately describe this issue. Consider the following snippet:
---
import std.traits : ParameterTypeTuple;
interface Bar {
int a(int t);
int b(short t);
}
template Foo(string name) {
mixin(
"alias ParameterTypeTuple!(Bar." ~ name ~ ")[0] Type;\n" ~
"alias const(Type)* PointerType;"
);
mixin("alias const(ParameterTypeTuple!(Bar." ~ name ~ ")[0])* PointerTypeDirect;");
pragma(msg, name, ": ", Type, ", ", PointerType, ", ", PointerTypeDirect);
}
// The second instantiation always prints the same for PointerTypeDirect as
// the first, try swapping them.
alias Foo!("a") FooA;
alias Foo!("b") FooB;
---
DMD from current Git master (ef2b5aa) prints:
---
a: int, const(int)*, int*
b: short, const(short)*, int*
---
Note that PointerTypeDirect is the same type on the second instantiation, while the version using an intermediary alias works fine. If the two instantiation lines are swapped, the output is as follows:
---
b: short, const(short)*, short*
a: int, const(int)*, short*
---
The two step version still works fine, whereas the direct version now has short* in both cases.
You might have noticed that it would be enough to use a string mixin expression for the ParameterTypeTuple argument instead of mixing in the whole declaration. If one replaces Foo by this:
---
template Foo(string name) {
alias const(ParameterTypeTuple!(mixin("Bar." ~ name))[0])* PointerTypeDirect;
pragma(msg, name, ": ", PointerTypeDirect);
}
---
The output is:
---
a: int*
b: short*
---
The const seems to be missing, but otherwise it seems to work – but if the original line is added in again:
---
template Foo(string name) {
mixin("alias const(ParameterTypeTuple!(Bar." ~ name ~ ")[0])* NotRelevant;");
alias const(ParameterTypeTuple!(mixin("Bar." ~ name))[0])* PointerTypeDirect;
pragma(msg, name, ": ", PointerTypeDirect);
}
---
The output again becomes:
---
a: int*
b: int*
---
If the original code is changed to declare a normal, non-const pointer, it works as expected as well:
---
template Foo(string name) {
mixin("alias ParameterTypeTuple!(Bar." ~ name ~ ")[0]* PointerTypeDirect;");
pragma(msg, name, ": ", PointerTypeDirect);
}
---
Correctly results in:
---
a: int*
b: short*
---
Comment #1 by kennytm — 2011-05-25T11:38:45Z
Reduced test case:
--------------------------------------
template Bug6056() {
mixin("alias const typeof('c') A; alias const typeof(0) B;");
static assert(is(B == int));
}
alias Bug6056!() Bug6056_dummy;
--------------------------------------
x.d(4): Error: static assert (is(char == int)) is false
x.d(7): instantiated from here: Bug6056!()
--------------------------------------
Alternative test case:
--------------------------------------
template Bug6056b() {
mixin("alias const(typeof('c')*) A; alias const(typeof(0)*) B;");
static assert(is(B == const(int*)));
}
alias Bug6056b!() Bug6056b_dummy;
--------------------------------------
Comment #2 by kennytm — 2011-05-25T11:39:51Z
Actually that 'template' is not needed.
------------------------
mixin("alias const(typeof('c')*) A; alias const(typeof(0)*) B;");
static assert(is(B == const(int*)));
------------------------
Comment #3 by k.hara.pg — 2011-10-04T11:39:25Z
string-mixin is also not needed.
alias const(typeof('c')*) A;
alias const(typeof(0)*) B;
static assert(is(B == const(int*)));
And template instance has same issue.
template X(T) { alias T X; }
alias const(X!char*) A;
alias const(X!int*) B;
static assert(is(B == const(int*)));
Patch:
https://github.com/D-Programming-Language/dmd/pull/430