Skip to content

Commit

Permalink
Fix 24056 - const uninitialized data at module scope is not in TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jul 25, 2023
1 parent 7ba1b40 commit 4a72388
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
9 changes: 9 additions & 0 deletions compiler/src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/mustuse.d
Original file line number Diff line number Diff line change
Expand Up @@ -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, "--");
Expand Down
8 changes: 6 additions & 2 deletions compiler/test/fail_compilation/fail4923.d
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 4a72388

Please sign in to comment.