Bug 20641 – [shared] static [~]this in templates get run multiple time when compiled separately

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-03-05T19:19:32Z
Last change time
2024-12-13T19:07:27Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Mathias LANG
Moved to GitHub: dmd#17943 →

Comments

Comment #0 by pro.mathias.lang — 2020-03-05T19:19:32Z
I was doing a bit of archaeology on the compiler to understand the source of the "gate" variable introduced in when a `[shared] static [~]this` is in a template constructor. This led me to [the initial implementation in DMD 2.015](https://github.com/dlang/dmd/commit/939fedf67d8a4b4d9f3f3982912fe1463e2484b8#diff-82ce41dbc2d224de66720dd0558a922eR672), then [the changelog entry](https://dlang.org/changelog/2.015.html), and finally issue #2146. Turns out the test case was not added to the testsuite when the bug was fixed. However, upon looking at the fix, the bug it introduces is apparent. While the following: ```D --- test/runnable/test2146.d import imports.test2146_b; alias Foo!(string) man; alias Foo!(int) foo; void main() { assert(counter == 2); } --- test/runnable/imports/test2146_b.d module imports.test2146_b; __gshared uint counter; template Foo(T) { static this() { ++counter; } } void baz() { alias Foo!(int) bar; alias Foo!(string) man; alias Foo!(int) over; } ``` Works just fine when compiling in one go, e.g. with the following command: ``` dmd -i -checkaction=context -Itest/runnable/ -run test/runnable/test2146.d ``` It falls short when separate compilation is used: ``` % dmd -checkaction=context -Itest/runnable/ -c test/runnable/test2146.d % dmd -checkaction=context -c test/runnable/imports/test2146_b.d % dmd test2146.o test2146_b.o % ./test2146 core.exception.AssertError@test/runnable/test2146.d(15): 4 != 2 ---------------- ??:? _d_assert_msg [0x10d6c0462] ??:? _Dmain [0x10d6b2dd0] ``` This looks somewhat similar to issue #14901 for which we have a test.
Comment #1 by pro.mathias.lang — 2020-03-06T03:37:38Z
For anyone willing to tackle this problem, it might be worth looking into what C++ is doing: http://itanium-cxx-abi.github.io/cxx-abi/abi.html#once-ctor
Comment #2 by robert.schadek — 2024-12-13T19:07:27Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17943 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB