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:
Dennis 2023-07-28 17:04:00 +02:00 committed by GitHub
parent 7db3a160a2
commit c3d0b1ff88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 60 additions and 22 deletions

View 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
}
---

View file

@ -188,6 +188,15 @@ bool modifyFieldVar(Loc loc, Scope* sc, VarDeclaration var, Expression e1)
MODtoChars(var.type.mod), var.kind(), var.toChars()); MODtoChars(var.type.mod), var.kind(), var.toChars());
errorSupplemental(loc, "Use `shared static this` instead."); 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; return result;
} }
else else

View file

@ -18,11 +18,11 @@ import dmd.identifier;
import dmd.location; import dmd.location;
// Used in isIncrementOrDecrement // Used in isIncrementOrDecrement
private static const StringExp plusPlus, minusMinus; private const StringExp plusPlus, minusMinus;
// Loc.initial cannot be used in static initializers, so // Loc.initial cannot be used in static initializers, so
// these need a static constructor. // these need a static constructor.
static this() shared static this()
{ {
plusPlus = new StringExp(Loc.initial, "++"); plusPlus = new StringExp(Loc.initial, "++");
minusMinus = new StringExp(Loc.initial, "--"); minusMinus = new StringExp(Loc.initial, "--");

View file

@ -1,13 +1,17 @@
/* /*
TEST_OUTPUT: TEST_OUTPUT:
--- ---
fail_compilation/fail4923.d(4): Error: immutable variable `bar` initialization is not allowed in `static this` fail_compilation/fail4923.d(5): 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): 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 #line 1
immutable int bar; immutable int bar;
const int baz; // https://issues.dlang.org/show_bug.cgi?id=24056
static this() static this()
{ {
bar = 42; bar = 42;
baz = 43;
} }

View file

@ -53,7 +53,7 @@ fail_compilation/fail66.d(59): Error: cannot modify `const` expression `x`
class C4 class C4
{ {
static const int x; static const int x;
static this() { x = 5; } shared static this() { x = 5; }
void foo() void foo()
{ {
x = 4; x = 4;
@ -67,7 +67,7 @@ fail_compilation/fail66.d(73): Error: cannot modify `const` expression `z5`
--- ---
*/ */
const int z5; const int z5;
static this() { z5 = 3; } shared static this() { z5 = 3; }
void test5() void test5()
{ {
z5 = 4; z5 = 4;

View file

@ -1991,7 +1991,7 @@ void test8976()
// https://issues.dlang.org/show_bug.cgi?id=8940 // https://issues.dlang.org/show_bug.cgi?id=8940
const int n8940; // or `immutable` const int n8940; // or `immutable`
static this() { n8940 = 3; } shared static this() { n8940 = 3; }
void f8940(T)(ref int val) void f8940(T)(ref int val)
{ {

View file

@ -1104,7 +1104,7 @@ class C60
{ {
} }
static this() shared static this()
{ {
x = 5; x = 5;
} }
@ -1117,7 +1117,7 @@ class C60
const int z60; const int z60;
static this() shared static this()
{ {
z60 = 3; z60 = 3;
} }

View file

@ -12,11 +12,11 @@ immutable int ig1;
static this() static this()
{ {
mg1 = 10; mg1 = 10;
cg1 = 10;
} }
shared static this() shared static this()
{ {
cg1 = 10;
ig1 = 10; ig1 = 10;
} }
static assert(!__traits(compiles, { static assert(mg1 == 0); })); static assert(!__traits(compiles, { static assert(mg1 == 0); }));

View file

@ -6159,7 +6159,7 @@ void test5332()
const int x11472 = void; const int x11472 = void;
static this() { x11472 = 10; } shared static this() { x11472 = 10; }
void test11472() void test11472()
{ {

View file

@ -361,7 +361,7 @@ struct CtorTest6174(Data)
const char gc6174; const char gc6174;
const char[1] ga6174; const char[1] ga6174;
static this() shared static this()
{ {
gc6174 = 'a'; // OK gc6174 = 'a'; // OK
ga6174[0] = 'a'; // line 5, Err ga6174[0] = 'a'; // line 5, Err
@ -726,7 +726,7 @@ struct Foo8783
const Foo8783[1] foos8783; const Foo8783[1] foos8783;
static this() shared static this()
{ {
foreach (i; 0 .. foos8783.length) foreach (i; 0 .. foos8783.length)
foos8783[i].bar[i] = 1; // OK foos8783[i].bar[i] = 1; // OK

View file

@ -551,7 +551,7 @@ void test38()
static const int x39; static const int x39;
const int y39; const int y39;
static this() shared static this()
{ {
x39 = 3; x39 = 3;
y39 = 4; y39 = 4;
@ -613,7 +613,7 @@ class C42
static const int d; static const int d;
static const int e = ctfe() + 2; static const int e = ctfe() + 2;
static this() shared static this()
{ {
d = 4; d = 4;
} }
@ -1302,7 +1302,7 @@ void test78()
const bool[string] stopWords79; const bool[string] stopWords79;
static this() shared static this()
{ {
stopWords79 = [ "a"[]:1 ]; stopWords79 = [ "a"[]:1 ];
} }

View file

@ -353,15 +353,11 @@ int foo15(int i)
return y; return y;
} }
static this()
{
X15 = 4;
Z15 = 5;
}
shared static this() shared static this()
{ {
X15 = 4;
Y15 = 4; Y15 = 4;
Z15 = 5;
} }
void test15() void test15()