www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4923] New: immutable module variables are modifiable in non-shared module constructors

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923

           Summary: immutable module variables are modifiable in
                    non-shared module constructors
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hanazuki gmail.com


--- Comment #0 from Kasumi Hanazuki <k.hanazuki gmail.com> 2010-09-23 12:05:20
PDT ---
DMD v2.049 on Windows

immutable module variables, whose instances are thread-global, are modifiable
in thread-local module constructors (static this).

Following example prints different numbers on each iteration.

----

import core.thread, std.random, std.stdio;

immutable int x;

static this() {
  x = unpredictableSeed;
}

void main() {
  for(;;) {
    (new Thread({ })).start;
    Thread.sleep(1_000_000_0);
    writeln(x);
  }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 23 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com


--- Comment #1 from nfxjfg gmail.com 2010-09-23 16:04:20 PDT ---
Isn't this by design?
Immutable variables are per-thread, and normal module constructors are used to
initialize per-thread data.
You need a way to initialize immutable variables.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 23 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923



--- Comment #2 from Kasumi Hanazuki <k.hanazuki gmail.com> 2010-09-23 18:25:30
PDT ---
I have no idea whether the spec states immutable module variables to be
allocated per thread or not,
but currently DMD places them as shared and there seems no way to store them in
TLS.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 23 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #3 from Jonathan M Davis <jmdavisProg gmx.com> 2011-01-04 00:26:06
PST ---
immutable variables are implicitly shared. That's part of the point of
immutable after all. And given that fact, allowing the initializing of
immutable variables in non-shared static constructors is definitely a bug. If
we decided that immutable variables were _not_ implicitly shared and made it so
that they weren't, then that would fix the problem, but that would on some
level defeat the purpose of immutable. So, if we are going to have immutable
variables be implicitly shared, then I propose that we disallow the
initializing of immutable variables with global scope in non-shared static
constructors. That is, all global variables and static variables which are
immutable _must_ be initialized in shared static constructors. The non-static
local variables would be okay (and I'm not sure that those actually end up
being implicitly shared anyway), since they'd be re-created on each function
call, but any immutable variable which could be initialized in a static
constructor would have to be initialized in a shared static constructor. I
don't really see any other viable way to fix this bug if we're going to
continue to have immutable variables be implicitly shared.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 04 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies gmail.com
           Platform|Other                       |All
         AssignedTo|nobody puremagic.com        |yebblies gmail.com
         OS/Version|Windows                     |All


--- Comment #4 from yebblies <yebblies gmail.com> 2012-02-01 16:57:05 EST ---
https://github.com/D-Programming-Language/dmd/pull/675

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 31 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4923



--- Comment #5 from github-bugzilla puremagic.com 2013-06-30 15:58:06 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/f5ed82d638b373efb6c2f0c77d64259df9472964
Merge pull request #1376 from yebblies/issue4923

initialize first thread's message box in a normal static ctor, to work around
requirement that thread-local variables cannot be set from a shared static
constructor.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 30 2013