www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 14901] New: [reg 2.067/2.068] template static shared this()


          Issue ID: 14901
           Summary: [reg 2.067/2.068] template static shared this() run
                    multiple times with separate compilation
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: code klickverbot.at

Consider the following test case (zip attached for your convenience):

module a;

extern(C) int printf(const char*, ...);

int make(string s)() {
    __gshared static int value;
    struct WithCtor {
        shared static this() {
            printf("%s\n", s.ptr);
    return value;
module b;

import a;

alias bar = make!"bar";

struct User(int id) {
    int foo() {
        return bar;
module c;

import b;

// This causes the id that is part of the generated name to be different.
shared static this() {}

void caller1() {
    User!1 u;
module d;

import b;

void caller2() {
    User!2 u;
module e;

import c;
import d;

void main() {

When compiled separately and with -unittest (e.g. using 'find *.d | xargs -n1
dmd -c -unittest' and then linking), it will print "bar" twice. This was likely
introduced in 2.067 (could not reproduce in 2.066), and still affects 2.068.

As far as the cause for this goes, for reasons I'm not entirely clear about a
unique identifier is used for function name of the static ctor (e.g.
_sharedStaticCtor55). For (amongst others) -unittest builds, the ctor function
is always emitted to fix issue 11239. Thus, different object files end up with
differently named copies of the same function, which consequently also gets run
multiple times.

Because the function name is also a part of the symbol name for the gate
boolean, the latter does not help with resolving this issue.

Aug 10 2015