Bug 21147 – Relax `shared` and `__gshared` check on static module constructor / destructors

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-10T02:42:57Z
Last change time
2024-12-13T19:10:55Z
Assigned to
No Owner
Creator
Mathias LANG
Moved to GitHub: dmd#19769 →

Comments

Comment #0 by pro.mathias.lang — 2020-08-10T02:42:57Z
Currently (v2.093.0), the following constructs are refused by the compiler: ``` shared int value; shared static this() { value += 1; } shared static ~this() { value -= 1; } ``` However, when those module ctor/dtor run, there is only one thread running, so initializing is safe. This is only possible for variables which are in the same module because shared static constructors *might* run when a shared library is loaded - breaking the single-thread assumption. However if the variable is in the same module the program cannot load the code and the variable separately, and while one could technically access the variable without running the initialization, it's not a use case we want to support. Likewise, the safety check for the following could be relaxed: ``` __gshared int value; shared static this() @safe { value += 1; } shared static ~this() @safe { value -= 1; } ```
Comment #1 by robert.schadek — 2024-12-13T19:10:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19769 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB