D-Programming-Language/dmd — pull request #2442 by 9rnsr (2013-08-01 14:08:18+00:00)
Title: [REG2.064] fix Issue 10736 - Regression (2.064 git-head): Instantiation failure triggered by module import and module order

http://d.puremagic.com/issues/show_bug.cgi?id=10736

The regression is introduced by recent [Phobos change](https://github.com/D-Programming-Language/phobos/pull/992), but the root cause is premature semantic3 invocation for template instances.

In the reduced test case, `test10736b` instantiates `chunks` template function, then it instantiates `Chunks!(int[])`. The instance object will be inserted to `test10736a` member because it is the first module that importing `test10736c`. But in this time, `test10736a` module's semantic3 is already done, then `Chunks!(int[])` would immediately invoke its semantic3, then `Chunks!(int[]).opSlice` refers *not yet instantiation finished* `chunks` template.

To avoid the forward reference problem, we should defer semantic3 of template instances which inserted to module scope. And after semantic3 done, insert it to the correct module member that it belongs to.

To do it I reuse `TemplateInstance::objFileModule` field that is used in GDC. I think the essential purpose of `objFileModule` field is same as the thing needed by issue 10736. @ibuclaw , could you check this change?