Thread overview
[Issue 6056] New: Type lookup problem in string mixins
May 25, 2011
klickverbot
May 25, 2011
kennytm@gmail.com
May 25, 2011
kennytm@gmail.com
Oct 04, 2011
Kenji Hara
Dec 11, 2011
Walter Bright
May 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6056

           Summary: Type lookup problem in string mixins
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: code@klickverbot.at


--- Comment #0 from klickverbot <code@klickverbot.at> 2011-05-25 09:22:04 PDT ---
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*
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6056


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com
           Platform|Other                       |x86
         OS/Version|Mac OS X                    |All


--- Comment #1 from kennytm@gmail.com 2011-05-25 11:38:45 PDT ---
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;
--------------------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6056



--- Comment #2 from kennytm@gmail.com 2011-05-25 11:39:51 PDT ---
Actually that 'template' is not needed.

------------------------
mixin("alias const(typeof('c')*) A; alias const(typeof(0)*) B;");
static assert(is(B == const(int*)));
------------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6056


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, rejects-valid


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2011-10-04 11:39:25 PDT ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 11, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6056


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2011-12-11 14:02:27 PST ---
https://github.com/D-Programming-Language/dmd/commit/bb3729ec6ccc4133368638069e5c09bfc035a77b

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------