https://issues.dlang.org/show_bug.cgi?id=14517
Issue ID: 14517
Summary: Templated static ctor + separate compilation = module
cycles
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: DMD
Assignee: [email protected]
Reporter: [email protected]
Even though druntime plays a role, I'm marking this as DMD, since druntime
cannot figure this out without compiler help.
Example:
mod1.d ==========
module mod1;
import std.stdio;
struct S(T)
{
shared static T t;
shared static this() {writeln(T.stringof); _t = 5;}
}
void main() {}
mod2.d ==========
module mod2;
import mod1;
import mod3;
S!int x;
mod3.d ==========
module mod3;
import mod1;
import mod2;
S!int x;
command line ====
dmd mod1.d mod2.d mod3.d
./mod1
int
dmd -c mod2.d
dmd -c mod3.d
dmd mod1.d mod2.o mod3.o
./mod1
object.Exception@src/rt/minfo.d(162): Aborting: Cycle detected between modules
with ctors/dtors:
mod2 -> mod3 -> mod2
================
Note that the module ctor only runs once (I only see one printout of 'int'), so
at least that is figured out at runtime.
Examination of the module info using druntime debugging printfs shows that in
the first case (when all 3 modules are compiled at once), mod1 is marked as
having a static CTOR, mod2 and 3 are not. In the second case, mod1 is not
marked as having a static ctor, mod2 and mod3 are.
I wonder if a separate "linkage" module info with just the static ctors
dependent on both modules should be created with a dependency on the
instantiating and the template module could be created, which should eliminate
any issues (it's definitely possible right now to construct a cycle between
instantiating and template modules). The instantiating module should have a
dependency on the linkage module as well, since there could be a cycle there
also! It's a very complex issue, but probably not very common.
--