www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21147] New: Relax `shared` and `__gshared` check on static

https://issues.dlang.org/show_bug.cgi?id=21147

          Issue ID: 21147
           Summary: Relax `shared` and `__gshared` check on static module
                    constructor / destructors
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: pro.mathias.lang gmail.com

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;
}
```

--
Aug 09 2020