Bug 4923 – immutable module variables are modifiable in non-shared module constructors
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-09-23T12:05:20Z
Last change time
2019-08-12T15:00:23Z
Keywords
accepts-invalid, pull
Assigned to
yebblies
Creator
Kasumi Hanazuki
Comments
Comment #0 by k.hanazuki — 2010-09-23T12:05:20Z
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);
}
}
Comment #1 by nfxjfg — 2010-09-23T16:04:20Z
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.
Comment #2 by k.hanazuki — 2010-09-23T18:25:30Z
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.
Comment #3 by issues.dlang — 2011-01-04T00:26:06Z
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.
An updated example, since Thread.sleep now requires that its argument be a core.time.Duration:
import core.thread, std.random, std.stdio;
immutable int x;
static this() {
x = unpredictableSeed;
}
void main() {
for(;;) {
(new Thread({ })).start;
Thread.sleep(seconds(1));
writeln(x);
}
}
Comment #7 by schveiguy — 2017-07-06T19:12:15Z
*** Issue 6114 has been marked as a duplicate of this issue. ***
Comment #8 by dlang-bot — 2019-05-09T09:47:11Z
@Geod24 created dlang/dmd pull request #9754 "Fix issue 4923: Deprecate modifying immutable variable from `static this`" fixing this issue:
- Fix issue 4923: Deprecate modifying immutable variable from `static this`
https://github.com/dlang/dmd/pull/9754
Comment #9 by dlang-bot — 2019-05-10T15:25:25Z
dlang/dmd pull request #9754 "Fix issue 4923: Deprecate modifying immutable variable from `static this`" was merged into master:
- 7c4ccb75c9aee7dfaac66c65fe0fb11d29bca8b9 by Geod24:
Fix issue 4923: Deprecate modifying immutable variable from `static this`
https://github.com/dlang/dmd/pull/9754