mirror of
https://github.com/dlang/dmd.git
synced 2025-04-27 05:30:13 +03:00

* Fix 24056 - const uninitialized data at module scope is not in TLS * Update tests * Add changelog entry
29 lines
745 B
Text
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
|
|
}
|
|
---
|