668 – Use of *.di files breaks the order of static module construction

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 668 - Use of *.di files breaks the order of static module construction
Summary: Use of *.di files breaks the order of static module construction
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 All
: P2 blocker
Assignee: Walter Bright
URL:
Keywords: wrong-code
: 1278 (view as issue list)
Depends on:
Blocks:
Reported: 2006年12月09日 10:28 UTC by Kazuhiro Inaba
Modified: 2014年02月15日 13:18 UTC (History)
3 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Kazuhiro Inaba 2006年12月09日 10:28:46 UTC
The spec says that "each module is assumed to depend on any
imported modules being statically constructed first" in module.html.
But when I use *.di interface files, the order seems broken.
Here is an example:
//credit goes to http://pc8.2ch.net/test/read.cgi/tech/1158013550/987
------- lib.d -------
bool initialized;
static this(){ initialized = true; } 
------- main.d -------
import lib;
import std.stdio;
void main(){ } 
static this(){ writefln("lib.initialized? ", lib.initialized); } 
> dmd -c -H lib.d // generating lib.di
> dmd main.d lib.obj
> main
lib.initialized? false // wrong
> del lib.di // deleteing b.di
> dmd main.d lib.obj
> main
lib.initialized? true // correct
Comment 1 Frits van Bommel 2006年12月10日 04:49:34 UTC
The likely cause:
-----
urxae@localhost:~/tmp$ cat lib.d
bool initialized;
static this(){ initialized = true; } 
urxae@localhost:~/tmp$ dmd -H -c lib.d
urxae@localhost:~/tmp$ cat lib.di
// D import file generated from 'lib.d'
bool initialized;
-----
The static constructor isn't present in the .di so the compiler doesn't see it when compiling main.d.
Confirmed on dmd-0.177/Linux.
Comment 2 Christian Kamm 2007年04月25日 02:20:13 UTC
Indeed, when you change lib.di to show the existance of a static constructor:
lib.di
---
// D import file generated from 'lib.d'
bool initialized;
static this();
---
Then it'll work as expected. Confirmed on dmd 1.013.
Comment 3 Sean Kelly 2007年06月18日 22:20:19 UTC
*** Bug 1278 has been marked as a duplicate of this bug. ***
Comment 4 clayasaurus 2007年07月03日 20:45:18 UTC
This bug is blocking the Arc v.2 release. 
Comment 5 Matti Niemenmaa 2007年07月04日 01:28:28 UTC
Raising severity in accordance with clayasaurus's comment.
Comment 6 Brad Roberts 2007年07月04日 01:51:26 UTC
Strictly speaking, since there's a work around, it's not a blocker. But it's fairly serious so I'll leave it as is.
Comment 7 clayasaurus 2007年07月16日 23:18:26 UTC
I'm using a work-around for now and I am going to release Arc v.2, but I still want this bug fixed! :)
Plus, this bug is high-severity for any DSSS installable software, because any DSSS library that uses static ctors will be broken. 
Comment 8 Christian Kamm 2007年07月23日 02:31:35 UTC
Fixed in DMD 1.019 and 2.003. Thanks Walter!
Comment 9 Sean Kelly 2007年08月06日 18:14:58 UTC
I don't think this issue is completely resolved. The compiler now generates a "static this() {}" in the .di file whenever it seems a static ctor in the module. However, the presence of a complete but empty static ctor seems to convince the compiler that they are available, inlinable and empty. In some instances, this causes errors like:
"filename.di(0): variable SOME_VARIABLE missing initializer in static constructor for const variable"
This is preventing the Tango user library from compiling properly on 1.019 and above for Linux/MacOS.
The solution seems to be generating "static this();" rather than the complete "static this() {}".
Comment 10 Walter Bright 2007年08月06日 20:06:00 UTC
Please provide a test case.
Comment 11 Sean Kelly 2007年08月06日 20:59:52 UTC
----
// Header.di
class Thread
{
 final char[] name();
 static const int PRIORITY_MIN;
 static const int PRIORITY_MAX;
 static Thread getThis();
 static this(){}
}
----
// Main.d
private import Header;
class Main
{
 void go()
 {
 char[] threadName = Thread.getThis.name;
 }
}
----
C:\> dmd -c -inline -release Main.d
The crucial bit is to have -inline and -release set. Clearing either of these eliminated the problem.
Comment 12 Brad Roberts 2007年10月20日 04:06:53 UTC
Fixed in 1.022/2.005


AltStyle によって変換されたページ (->オリジナル) /