Bug 7220 – Bad initialization when using mixin to generate a static field in a -lib'rary

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-01-04T10:17:00Z
Last change time
2015-06-09T05:11:55Z
Assigned to
nobody
Creator
alex

Comments

Comment #0 by alex — 2012-01-04T10:17:29Z
main.d: module main; import std.stdio, faulty; static this() { writefln("Faulty.instance: %s", Faulty.instance); } private int main(string[] args) { writeln("Main"); return 0; } faulty.d: module faulty; import std.stdio; private mixin template DefineCoreType(string type) { mixin("public final class " ~ type ~ "{" ~ " private static " ~ type ~ " _instance;" ~ "" ~ " @property public static " ~ type ~ " instance()" ~ " {" ~ " writeln(\"accessing " ~ type ~ ".instance()\");"~ " return _instance; " ~ " }" ~ "" ~ " static this()" ~ " {" ~ " writeln(\"initializing " ~ type ~"._instance\");"~ " _instance = new " ~ type ~ "();" ~ " }" ~ "" ~ "}"); } mixin DefineCoreType!("Faulty"); Compile with: dmd -lib faulty.d dmd main.d faulty.lib Run the resulting main.exe, and you'll see this output: accessing Faulty.instance() Faulty.instance: null initializing Faulty._instance Main but the expected output is: initializing Faulty._instance accessing Faulty.instance() Faulty.instance: faulty.DefineCoreType!("Faulty").Faulty Main The error does not occur if faulty.d is not compiled as a -lib'rary (i.e. compiled directly into the executable), or if you unroll the mixin so faulty.d becomes like so: module faulty; import std.stdio; // this works properly! public final class Faulty { private static Faulty _instance; @property public static Faulty instance() { writeln("accessing Faulty.instance()"); return _instance; } static this() { writeln("initializing Faulty._instance"); _instance = new Faulty(); } }
Comment #1 by lovelydear — 2012-04-19T12:52:30Z
Confirmed with 2.059 on win32
Comment #2 by turkeyman — 2012-11-12T05:53:16Z
I'm seeing symptoms that look like this bug. I have a mixin template introducing shared static this() into many of my modules, and only some of them are called on startup. Many of them are simply not called, and I crash on uninitialised data. Latest DMD - Win64, building a DLL.
Comment #3 by bugzilla — 2012-11-12T20:24:17Z
This gets quite a bit simpler: ---------- faulty.d ------------- module faulty; import core.stdc.stdio; mixin template DefineCoreType(string type) { class Faulty { static int x; static void instance() { printf("Faulty.instance()\n"); x = 3; } static this() { printf("Faulty.static this\n"); } } } mixin DefineCoreType!("Faulty"); ---------- bar.d ------------------------- import core.stdc.stdio, faulty; static this() { Faulty.instance(); assert(Faulty.x == 3); printf("bar.static this\n"); } private int main(string[] args) { printf("Main\n"); return 0; } ------------------------------------------ dmd -lib faulty dmd bar faulty.lib bar
Comment #4 by github-bugzilla — 2012-11-12T23:43:47Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/af887b44f3bd592973037a2dc8f5ec91cc8b2626 fix Issue 7220 - Bad initialization when using mixin to generate a static field in a -lib'rary
Comment #5 by github-bugzilla — 2012-11-16T13:49:29Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/0cb89561a45edd46440d51e5807e4e1434735d24 fix Issue 7220 - Bad initialization when using mixin to generate a static field in a -lib'rary