template instantiation problems

berni44 dlang at d-ecke.de
Fri Jan 10 17:27:10 UTC 2020


I'm trying to understand issue 17441 [1]. I reduced it (including 
Phobos) to the following (and added some message to point out the 
problem):

test.d:
---
alias parentOf(alias sym) = __traits(parent, sym);

template packageName(alias T)
{
     pragma(msg, "IN: ", T.stringof);
     static if (__traits(compiles, parentOf!T))
         enum parent = packageName!(parentOf!T);
     else
         enum string parent = null;

     static if (T.stringof[0..8] == "package ")
         enum packageName = (parent? parent ~ '.' : "") ~ 
T.stringof[8 .. $];
     else
         enum packageName = parent;
}

void main()
{
     import mod0 = foo.baz;
     import mod1 = foo;

     pragma(msg, "mod0: ",mod0.stringof);
     pragma(msg, "mod1: ",mod1.stringof);

     pragma(msg, "A");
     enum a = packageName!mod0;
     pragma(msg, "B");
     enum b = packageName!mod1;
     pragma(msg, "C");

     static assert(a == "foo");
     static assert(b == "");      // Error: static assert:  "foo" 
== "" is false
}
---

foo/baz.d:
---
module foo.baz;
---

foo/package.d:
---
module foo;
---

When compiling with

$> dmd test.d

I get:

---
mod0: module baz
mod1: module foo
A
IN: module baz
IN: package foo
B
C
test.d(32): Error: static assert:  "foo" == "" is false
---

This clearly shows, that packageName is only instatiated once at 
top level although mod0 and mod1 are two different things (at 
least their stringof produces different results) and therefore 
should be instantiated twice.

Now I don't know if this is a bug in DMD or I should know 
something I do not know... Can you help me?

[1] https://issues.dlang.org/show_bug.cgi?id=17441


More information about the Digitalmars-d-learn mailing list