Bug 1392 – Template instantiation fails with circular import

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2007-07-31T15:26:00Z
Last change time
2014-02-16T15:26:26Z
Keywords
rejects-valid
Assigned to
nobody
Creator
cdunn2001

Comments

Comment #0 by cdunn2001 — 2007-07-31T15:26:58Z
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.)
Comment #1 by cdunn2001 — 2007-07-31T15:27:50Z
Oh, I should add that separate module compilation works fine when Foo is not a template.
Comment #2 by cdunn2001 — 2007-07-31T15:47:05Z
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?!
Comment #3 by cdunn2001 — 2007-07-31T16:02:37Z
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.
Comment #4 by lovesyao — 2007-07-31T16:10:55Z
Reply to [email protected], > http://d.puremagic.com/issues/show_bug.cgi?id=1392 > > ------- Comment #3 from [email protected] 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
Comment #5 by cdunn2001 — 2007-07-31T16:45:00Z
Choice of build-tool is a separate issue. I am interested in the command-line.
Comment #6 by smjg — 2007-10-21T11:30:48Z
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
Comment #7 by smjg — 2007-10-29T08:06:50Z
*** Bug 1538 has been marked as a duplicate of this bug. ***
Comment #8 by r.sagitario — 2009-09-18T01:48:53Z
I could not reproduce this bug with DMD 1.047 and DMD 2.032.
Comment #9 by clugdbug — 2009-09-18T15:13:15Z
Still failed in 1.041, but working in 1.047.