dmd/changelog/dmd.shared-static-ctor-const.dd
Dennis c3d0b1ff88
Fix 24056 - const uninitialized data at module scope is not in TLS (#15458)
* Fix 24056 - const uninitialized data at module scope is not in TLS

* Update tests

* Add changelog entry
2023-07-28 17:04:00 +02:00

29 lines
745 B
Text

Global `const` variables can no longer be initialized from a non-shared static constructor
Just like `immutable` data, global `const` data is not placed in Thread Local Storage (TLS), so initializing it in a thread-local static constructor allows you to violate `const`:
see [issue 24056](https://issues.dlang.org/show_bug.cgi?id=24056) for details.
Doing this will now result in a deprecation:
---
int x;
const int y;
immutable int z;
static this()
{
x = 1;
y = 2; // Deprecation: cannot modify const variable
z = 3; // Error: cannot modify immutable variable (same as before)
}
---
As a corrective action, move the initialization to a shared static constructor:
---
const int y;
shared static this()
{
y = 4; // OK
}
---