www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20641] New: [shared] static [~]this in templates get run


          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
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:
--- 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()

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