Bug 6113 – singletons in std.datetime are not created early enough
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-06-05T22:24:00Z
Last change time
2011-12-25T18:14:05Z
Assigned to
issues.dlang
Creator
jsancio
Comments
Comment #0 by jsancio — 2011-06-05T22:24:28Z
When accessing the singletons from a shared static this module ctr, the singleton have not been instantiated. The following code succeeds when it shouldn't:
import std.datetime;
shared static this()
{
assert(UTC() is null);
assert(LocalTime() is null);
}
A possible solution is:
class Test
{
shared static this() { _obj = new immutable(shared(Object)); }
shared static immutable Object _ojb;
}
shared static this()
{
assert(Test._ojb !is null);
}
While changing them to shared static ctors is a good change, I there might well be a real bug here anyway.
For the main thread's initialization, should it run through all shared static ctors then all non-shared static ctors as two passes? Or, should it run through all shared static and non-shared statics together in one pass?
Comment #3 by issues.dlang — 2011-06-06T00:13:27Z
That's why I opened bug# 6114. I'm going to close this one once my fix has been merged in, since that fixes the problem with std.datetime. But there _is_ a compiler bug here. Bug# 6114 covers that.
Everything that I've read says that the static constructors are supposed supposed to be run in lexical order within a module, and the the compiler will order the initialization of the modules such that they're run in the order necessary to initialize everything before it's used. I haven't seen anything that would indicate that hared static constructors should be treated any differently from other static constructors as far as initialization order goes.
Why was this closed? The following code still failed:
import std.datetime;
shared static this()
{
assert(UTC() !is null);
assert(LocalTime() !is null);
}
void main() {}
Comment #6 by jsancio — 2011-09-04T23:41:09Z
While we are on the subject, does anyone have a suggestion on how to write unit test that test this? Or do we need to extend the unittest capability?
Comment #7 by issues.dlang — 2011-09-09T00:47:41Z
This _was_ fixed. If it's broken again, it's likely a bug in dmd or druntime related to the order of static constructors. It's probably releated to bug# 6631.
Comment #8 by issues.dlang — 2011-12-25T18:14:05Z
Well, like I said, this was fixed previously and is only a problem in the current release due to bug# 6331. However, in git, UTC and LocalTime have been changed to be lazily loaded thanks to some casting hackery, so this is no longer an issue regardless of the state of bug# 6331.