relevant function: https://github.com/dlang/dmd/blob/0e3e0f9/src/dmd/cparse.d#L4437
static is ignored for thread-locals at top level, so the symbol isn't made static
// file1.c
static _Thread_local int tmp;
// file2.c
static _Thread_local int tmp;
int main(){}
compile: "dmd file1.c file2.c", gives a multiple definition error for "tmp" even though it should be static
the parser does apply both static and _Thread_local when inside a function, but that doesn't seem to be supported/working:
int printf(char *, ...);
int main()
{
static _Thread_local int x = 1;
printf("%p\n", &x);
printf(" %d\n", x);
}
asserts when compiling with this output:
e.Eoper: '&'
el:0x562029f25560 cnt=0 cs=0 & TY* 0x562029f25500
el:0x562029f25500 cnt=0 cs=0 call TYint 0x562029f254a0 0x562029f250e0
el:0x562029f254a0 cnt=0 cs=0 var TYC func __tls_get_addr
el:0x562029f250e0 cnt=0 cs=0 relconst mTYvolatile|TY* 0+& _D4test4mainUY1xi
---
ERROR: This is a compiler bug.
Please report it via https://issues.dlang.org/enter_bug.cgi
with, preferably, a reduced, reproducible example and the information below.
DustMite (https://github.com/CyberShadow/DustMite/wiki) can help with the reduction.
---
DMD v2.099.1-351-g26299b627
predefs DigitalMars LittleEndian D_Version2 all D_SIMD Posix ELFv1 linux CRuntime_Glibc CppRuntime_Gcc D_InlineAsm_X86_64 X86_64 D_LP64 D_PIC assert D_PreConditions D_PostConditions D_Invariants D_ModuleInfo D_Exceptions D_TypeInfo D_HardFloat
binary /home/human/work/result/bin/dmd
version v2.099.1-351-g26299b627
config /home/human/work/result/bin/dmd.conf
DFLAGS -I/home/human/work/result/bin/../import -L-L/home/human/work/result/bin/../lib -L--export-dynamic -fPIC
---
core.exception.AssertError@src/dmd/backend/cgcs.d(442): Assertion failure
----------------
??:? onAssertError [0x7fae73628622]
src/dmd/backend/cgcs.d:442 nothrow @trusted void dmd.backend.cgcs.ecom(ref dmd.backend.cgcs.CGCS, ref dmd.backend.el.elem*) [0x562028c21fdb]
src/dmd/backend/cgcs.d:371 nothrow @trusted void dmd.backend.cgcs.ecom(ref dmd.backend.cgcs.CGCS, ref dmd.backend.el.elem*) [0x562028c21d5b]
src/dmd/backend/cgcs.d:338 nothrow @trusted void dmd.backend.cgcs.ecom(ref dmd.backend.cgcs.CGCS, ref dmd.backend.el.elem*) [0x562028c21cc0]
src/dmd/backend/cgcs.d:127 nothrow @trusted void dmd.backend.cgcs.comsubs2(dmd.backend.cc.block*, ref dmd.backend.cgcs.CGCS) [0x562028c215ff]
src/dmd/backend/cgcs.d:60 _Z7comsubsv [0x562028c214c5]
src/dmd/backend/blockopt.d:717 _Z8blockopti [0x562028beff57]
src/dmd/backend/out.d:1409 _Z10writefunc2P6Symbol [0x562028c03a35]
src/dmd/backend/out.d:1137 _Z9writefuncP6Symbol [0x562028c03154]
src/dmd/glue.d:1184 _Z25FuncDeclaration_toObjFileP15FuncDeclarationb [0x562028af6d79]
src/dmd/toobj.d:311 _ZN9toObjFile9ToObjFile5visitEP15FuncDeclaration [0x562028b0c698]
src/dmd/func.d:2833 _ZN15FuncDeclaration6acceptEP7Visitor [0x5620288d4ba7]
src/dmd/toobj.d:748 _ZN9toObjFile9ToObjFile5visitEP17AttribDeclaration [0x562028b0beef]
??:? _ZN16ParseTimeVisitorI10ASTCodegenE5visitEP15LinkDeclaration [0x562028b1cea3]
src/dmd/attrib.d:433 _ZN15LinkDeclaration6acceptEP7Visitor [0x5620288d5158]
src/dmd/toobj.d:1008 _Z9toObjFileP7Dsymbolb [0x5620288fe808]
src/dmd/glue.d:522 _Z10genObjFileP6Moduleb [0x562028af1e3c]
src/dmd/glue.d:120 void dmd.glue.generateCodeAndWrite(dmd.dmodule.Module[], const(char)*[], const(char)[], const(char)[], bool, bool, bool, bool, bool) [0x56202890cb8b]
src/dmd/mars.d:574 int dmd.mars.tryMain(ulong, const(char)**, ref dmd.globals.Param) [0x56202890f602]
src/dmd/mars.d:958 _Dmain [0x56202891afa6]
Comment #1 by dlang-bot — 2023-04-02T04:56:08Z
@WalterBright created dlang/dmd pull request #15069 "fix Issue 23014 - importC: static thread-locals do not work" fixing this issue:
- fix Issue 23014 - importC: static thread-locals do not work
https://github.com/dlang/dmd/pull/15069
Comment #2 by dlang-bot — 2023-04-04T10:16:06Z
dlang/dmd pull request #15069 "fix Issue 23014 - importC: static thread-locals do not work" was merged into master:
- 58baa9ef23e01943e2df1985f2e8dfbc38aa7f0b by Walter Bright:
fix Issue 23014 - importC: static thread-locals do not work
https://github.com/dlang/dmd/pull/15069
Comment #3 by dlang-bot — 2023-05-04T12:17:19Z
dlang/dmd pull request #15170 "fix issues 20737 and 23014 - TLS variables unusable with -betterC/imp…" was merged into stable:
- b4fe7b84643c9638ff22f06f614b907a67551a62 by Rainer Schuetze:
fix issues 20737 and 23014 - TLS variables unusable with -betterC/importC for Windows MSVC targets
always generate access via __tls_index
https://github.com/dlang/dmd/pull/15170
Comment #4 by dlang-bot — 2023-05-16T12:28:55Z
dlang/dmd pull request #15238 "merge stable" was merged into master:
- 3072e43d2b60a85b219c7572557c0e367e8d4a88 by Rainer Schuetze:
fix issues 20737 and 23014 - TLS variables unusable with -betterC/importC for Windows MSVC targets (#15170)
always generate access via __tls_index
https://github.com/dlang/dmd/pull/15238