D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1392 - Template instantiation fails with circular import
Summary: Template instantiation fails with circular import
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
: 1538 (view as issue list)
Depends on:
Blocks:
 
Reported: 2007-07-31 15:26 UTC by C. Dunn
Modified: 2014-02-16 15:26 UTC (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description C. Dunn 2007-07-31 15:26:58 UTC
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 C. Dunn 2007-07-31 15:27:50 UTC
Oh, I should add that separate module compilation works fine when Foo is not a template.
Comment 2 C. Dunn 2007-07-31 15:47:05 UTC
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 C. Dunn 2007-07-31 16:02:37 UTC
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 Nazo Humei 2007-07-31 16:10:55 UTC
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


Comment 5 C. Dunn 2007-07-31 16:45:00 UTC
Choice of build-tool is a separate issue.  I am interested in the command-line.
Comment 6 Stewart Gordon 2007-10-21 11:30:48 UTC
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 Stewart Gordon 2007-10-29 08:06:50 UTC
*** Bug 1538 has been marked as a duplicate of this bug. ***
Comment 8 Rainer Schuetze 2009-09-18 01:48:53 UTC
I could not reproduce this bug with DMD 1.047 and DMD 2.032.
Comment 9 Don 2009-09-18 15:13:15 UTC
Still failed in 1.041, but working in 1.047.