mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 13:10:12 +03:00
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
This commit is contained in:
parent
7db3a160a2
commit
c3d0b1ff88
12 changed files with 60 additions and 22 deletions
29
changelog/dmd.shared-static-ctor-const.dd
Normal file
29
changelog/dmd.shared-static-ctor-const.dd
Normal file
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
}
|
||||
---
|
|
@ -188,6 +188,15 @@ bool modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1)
|
|||
MODtoChars(var.type.mod), var.kind(), var.toChars());
|
||||
errorSupplemental(loc, "Use `shared static this` instead.");
|
||||
}
|
||||
else if (fd.isStaticCtorDeclaration() && !fd.isSharedStaticCtorDeclaration() &&
|
||||
var.type.isConst())
|
||||
{
|
||||
// @@@DEPRECATED_2.116@@@
|
||||
// Turn this into an error, merging with the branch above
|
||||
.deprecation(loc, "%s %s `%s` initialization is not allowed in `static this`",
|
||||
MODtoChars(var.type.mod), var.kind(), var.toChars());
|
||||
deprecationSupplemental(loc, "Use `shared static this` instead.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else
|
||||
|
|
|
@ -18,11 +18,11 @@ import dmd.identifier;
|
|||
import dmd.location;
|
||||
|
||||
// Used in isIncrementOrDecrement
|
||||
private static const StringExp plusPlus, minusMinus;
|
||||
private const StringExp plusPlus, minusMinus;
|
||||
|
||||
// Loc.initial cannot be used in static initializers, so
|
||||
// these need a static constructor.
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
plusPlus = new StringExp(Loc.initial, "++");
|
||||
minusMinus = new StringExp(Loc.initial, "--");
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
/*
|
||||
TEST_OUTPUT:
|
||||
---
|
||||
fail_compilation/fail4923.d(4): Error: immutable variable `bar` initialization is not allowed in `static this`
|
||||
fail_compilation/fail4923.d(4): Use `shared static this` instead.
|
||||
fail_compilation/fail4923.d(5): Error: immutable variable `bar` initialization is not allowed in `static this`
|
||||
fail_compilation/fail4923.d(5): Use `shared static this` instead.
|
||||
fail_compilation/fail4923.d(6): Deprecation: const variable `baz` initialization is not allowed in `static this`
|
||||
fail_compilation/fail4923.d(6): Use `shared static this` instead.
|
||||
---
|
||||
*/
|
||||
#line 1
|
||||
immutable int bar;
|
||||
const int baz; // https://issues.dlang.org/show_bug.cgi?id=24056
|
||||
static this()
|
||||
{
|
||||
bar = 42;
|
||||
baz = 43;
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ fail_compilation/fail66.d(59): Error: cannot modify `const` expression `x`
|
|||
class C4
|
||||
{
|
||||
static const int x;
|
||||
static this() { x = 5; }
|
||||
shared static this() { x = 5; }
|
||||
void foo()
|
||||
{
|
||||
x = 4;
|
||||
|
@ -67,7 +67,7 @@ fail_compilation/fail66.d(73): Error: cannot modify `const` expression `z5`
|
|||
---
|
||||
*/
|
||||
const int z5;
|
||||
static this() { z5 = 3; }
|
||||
shared static this() { z5 = 3; }
|
||||
void test5()
|
||||
{
|
||||
z5 = 4;
|
||||
|
|
|
@ -1991,7 +1991,7 @@ void test8976()
|
|||
// https://issues.dlang.org/show_bug.cgi?id=8940
|
||||
|
||||
const int n8940; // or `immutable`
|
||||
static this() { n8940 = 3; }
|
||||
shared static this() { n8940 = 3; }
|
||||
|
||||
void f8940(T)(ref int val)
|
||||
{
|
||||
|
|
|
@ -1104,7 +1104,7 @@ class C60
|
|||
{
|
||||
}
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
x = 5;
|
||||
}
|
||||
|
@ -1117,7 +1117,7 @@ class C60
|
|||
|
||||
const int z60;
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
z60 = 3;
|
||||
}
|
||||
|
|
|
@ -12,11 +12,11 @@ immutable int ig1;
|
|||
static this()
|
||||
{
|
||||
mg1 = 10;
|
||||
cg1 = 10;
|
||||
}
|
||||
|
||||
shared static this()
|
||||
{
|
||||
cg1 = 10;
|
||||
ig1 = 10;
|
||||
}
|
||||
static assert(!__traits(compiles, { static assert(mg1 == 0); }));
|
||||
|
|
|
@ -6159,7 +6159,7 @@ void test5332()
|
|||
|
||||
const int x11472 = void;
|
||||
|
||||
static this() { x11472 = 10; }
|
||||
shared static this() { x11472 = 10; }
|
||||
|
||||
void test11472()
|
||||
{
|
||||
|
|
|
@ -361,7 +361,7 @@ struct CtorTest6174(Data)
|
|||
|
||||
const char gc6174;
|
||||
const char[1] ga6174;
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
gc6174 = 'a'; // OK
|
||||
ga6174[0] = 'a'; // line 5, Err
|
||||
|
@ -726,7 +726,7 @@ struct Foo8783
|
|||
|
||||
const Foo8783[1] foos8783;
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
foreach (i; 0 .. foos8783.length)
|
||||
foos8783[i].bar[i] = 1; // OK
|
||||
|
|
|
@ -551,7 +551,7 @@ void test38()
|
|||
static const int x39;
|
||||
const int y39;
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
x39 = 3;
|
||||
y39 = 4;
|
||||
|
@ -613,7 +613,7 @@ class C42
|
|||
static const int d;
|
||||
static const int e = ctfe() + 2;
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
d = 4;
|
||||
}
|
||||
|
@ -1302,7 +1302,7 @@ void test78()
|
|||
|
||||
const bool[string] stopWords79;
|
||||
|
||||
static this()
|
||||
shared static this()
|
||||
{
|
||||
stopWords79 = [ "a"[]:1 ];
|
||||
}
|
||||
|
|
|
@ -353,15 +353,11 @@ int foo15(int i)
|
|||
return y;
|
||||
}
|
||||
|
||||
static this()
|
||||
{
|
||||
X15 = 4;
|
||||
Z15 = 5;
|
||||
}
|
||||
|
||||
shared static this()
|
||||
{
|
||||
X15 = 4;
|
||||
Y15 = 4;
|
||||
Z15 = 5;
|
||||
}
|
||||
|
||||
void test15()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue