D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6056 - Type lookup problem in string mixins
Summary: Type lookup problem in string mixins
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 All
: P2 major
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2011-05-25 09:22 UTC by David Nadlinger
Modified: 2011-12-11 14:02 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Nadlinger 2011-05-25 09:22:04 UTC
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 kennytm 2011-05-25 11:38:45 UTC
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 kennytm 2011-05-25 11:39:51 UTC
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 Kenji Hara 2011-10-04 11:39:25 UTC
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