D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6113 - singletons in std.datetime are not created early enough
Summary: singletons in std.datetime are not created early enough
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: Jonathan M Davis
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-05 22:24 UTC by Jose Garcia
Modified: 2011-12-25 18:14 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jose Garcia 2011-06-05 22:24:28 UTC
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);
}
Comment 1 Jonathan M Davis 2011-06-05 23:37:20 UTC
There's now a pull request with the fixes: https://github.com/D-Programming-Language/phobos/pull/81

Given the simplicity of the change, I expect that it'll be merged in fairly quickly.
Comment 2 Brad Roberts 2011-06-06 00:00:14 UTC
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 Jonathan M Davis 2011-06-06 00:13:27 UTC
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.
Comment 4 Jonathan M Davis 2011-06-08 09:01:06 UTC
Fixed as far as std.datetime goes: https://github.com/D-Programming-Language/phobos/commit/ba7d70b85802e5626708545c96e38f91fcf5c341

The underlying problem ( bug# 6114 ) still exists though.
Comment 5 Jose Garcia 2011-09-04 23:38:06 UTC
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 Jose Garcia 2011-09-04 23:41:09 UTC
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 Jonathan M Davis 2011-09-09 00:47:41 UTC
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 Jonathan M Davis 2011-12-25 18:14:05 UTC
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.