fooA.d ------ import fooFoo; class A{ static int hi(){ //auto x = new F; //F.there(); return 0; } //Foo!(char) x; }; Foo!(char)* x; --- fooFoo.d -------- import fooA; class Foo(T){ //A* bar(){ // return null; //return A.hi(); //} int i; }; --- I've commented out anything which could confuse the issue. The problem is strictly the circular 'import' of the module which defines the template and the module which specializes it, independent of whether Foo and A actually depend on each other. If these are in the same module, it compiles, even with all the extra stuff, and even with refs instead of pointers. combined.d ---------- alias Foo!(char) F; class A{ static int hi(){ auto x = new F; F.there(); return 0; } F x; }; class Foo(T){ A bar(){ A.hi(); return new A; } int i; }; --- In fact, it works if I compile them at the same time, with: dmd -c fooA.d fooFoo.d Interestingly, this produces two object files, fooA.o and fooFoo.o, which can then be linked into an executable. So the problem comes from compiling just fooFoo.d into fooFoo.o: dmd -c fooFoo.d fooA.d(11): template instance forward reference to template declaration Foo(T) fooA.d(11): Error: Foo!(char) is used as a type In other words, this is not a bug in the language, but rather a bug in compilation. Since I can work around it by just compiling all modules in a package simultaneously, it is not a critical issue, but if it is not fixed, I think people will run into this problem repeatedly. It might make compilation slower too, since in practice, any change will force recompilation of all modules. Please let me know if you need more details. (This is perhaps related to Bug 805 and #877.)
Oh, I should add that separate module compilation works fine when Foo is not a template.
This gets weirder and weirder. This works: dmd -c fooA.d fooFoo.d but this does not: dmd -c fooFoo.d fooA.d fooA.d(10): template instance forward reference to template declaration Foo(T) fooA.d(10): Error: Foo!(char) is used as a type The order of files on the compilation line makes a difference?!
And since this fails: dmd -c fooFoo.d fooA.d(10): template instance forward reference to template declaration Foo(T) fooA.d(10): Error: Foo!(char) is used as a type (and note that the error is listed as being in the other module) it becomes impossible to write an implicit rule in a Makefile. fooFoo.o: fooA.d fooFoo.d %.o: %.d dmd -c $^ make fooFoo.o dmd -c fooFoo.d fooA.d fooA.d(10): template instance forward reference to template declaration Foo(T) fooA.d(10): Error: Foo!(char) is used as a type So I have to add a special rule in my Makefile: fooFoo.o: fooA.d fooFoo.d ${DC} -c $^ in order to get the files listed in the correct order.
Reply to d-bugmail@puremagic.com, > http://d.puremagic.com/issues/show_bug.cgi?id=1392 > > ------- Comment #3 from cdunn2001@gmail.com 2007-07-31 16:02 ------- > And since this fails: > dmd -c fooFoo.d > fooA.d(10): template instance forward reference to template > declaration > Foo(T) > fooA.d(10): Error: Foo!(char) is used as a type > (and note that the error is listed as being in the other module) > it becomes impossible to write an implicit rule in a Makefile. > > fooFoo.o: fooA.d fooFoo.d > %.o: %.d > dmd -c $^ > make fooFoo.o > dmd -c fooFoo.d fooA.d > fooA.d(10): template instance forward reference to template > declaration > Foo(T) > fooA.d(10): Error: Foo!(char) is used as a type > So I have to add a special rule in my Makefile: > > fooFoo.o: fooA.d fooFoo.d > ${DC} -c $^ > in order to get the files listed in the correct order. > have you tried bud or rebuild? http://www.dsource.org/projects/build/ http://www.dsource.org/projects/dsss/wiki/Rebuild
Choice of build-tool is a separate issue. I am interested in the command-line.
Minimal testcase: ----- bz1392a.d ----- import bz1392b; Foo!(char)* x; ----- bz1392b.d ----- import bz1392a; class Foo(T) {} ---------- Success: dmd bz1392a.d dmd bz1392a.d bz1392b.d Failure: dmd bz1392b.d dmd bz1392b.d bz1392a.d bz1392a.d(3): template instance forward reference to template declaration Foo(T) bz1392a.d(3): Error: Foo!(char) is used as a type
*** Bug 1538 has been marked as a duplicate of this bug. ***
I could not reproduce this bug with DMD 1.047 and DMD 2.032.
Still failed in 1.041, but working in 1.047.