digitalmars.D.bugs - [Issue 20641] New: [shared] static [~]this in templates get run
- d-bugmail puremagic.com (68/68) Mar 05 2020 https://issues.dlang.org/show_bug.cgi?id=20641
https://issues.dlang.org/show_bug.cgi?id=20641 Issue ID: 20641 Summary: [shared] static [~]this in templates get run multiple time when compiled separately Product: D Version: D2 Hardware: All OS: All Status: NEW Keywords: wrong-code Severity: major Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: pro.mathias.lang gmail.com 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 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] ``` --
Mar 05 2020