D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6631 - core.time module constructor runs AFTER main program's module constructor
Summary: core.time module constructor runs AFTER main program's module constructor
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-08 17:30 UTC by Vladimir Panteleev
Modified: 2012-04-27 14:42 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2011-09-08 17:30:31 UTC
(This might be a DMD bug)

import core.time;
static this()
{
	assert(TickDuration.ticksPerSec != 0);
}
void main() {}

The above will fail, because the core.time module constructor will not have ran when the check is performed. The assert will pass if moved inside main().
Comment 1 Steven Schveighoffer 2011-09-09 04:33:45 UTC
This is truly a bug somewhere, because your static ctor is not shared, yet the static ctor that sets ticspersec is shared. Shared static ctors should be run *before* non-shared ones, so this has nothing to do with dependencies.

Examining the generated assembly, it's identical whether you include it in main or static this, and it does access the actual member, not some optimized-out constant.

So I think it *must* be running the shared ctor *after* the unshared one.  This is likely not a problem with druntime, but rather the compiler.  I'll try to investigate further to see why the module is run later.  Perhaps the @trusted is giving dmd fits on the shared ctor.  But even so, core.time should be a dependency of the main program module, so it should still be run first.
Comment 2 Vladimir Panteleev 2011-09-09 05:27:48 UTC
Note that adding "shared" to the main module constructor doesn't change the situation.
Comment 3 Steven Schveighoffer 2011-09-09 08:05:25 UTC
Right, what I was saying though is that all shared ctors are run before all non-shared ones.  So it is very surprising that a shared ctor has *not* been run before a non-shared one, regardless of dependency.
Comment 4 SomeDude 2012-04-27 14:37:45 UTC
Compiles and runs with 2.059 Win32
Comment 5 Vladimir Panteleev 2012-04-27 14:42:25 UTC
Indeed. Seems to be fixed.