void func(int i)
{
switch(i)
{
case 1:{static int j;break;}
case 2:{static int j;break;}
}
}
void main()
{
}
compile this you get the previous definition different error when you link it.
Error 1: Previous Definition Different : _D4test4funcFiZv1ji
Comment #1 by peter.alexander.au — 2010-10-23T12:39:50Z
Simpler test case:
----
void main()
{
{ static int foo; }
{ static int foo; }
}
$ rdmd test.d
ld: duplicate symbol _D4test4mainFZv3fooi in /tmp/.rdmd/rdmd-test.d-801BCEFE1CAD8F2472CC2713A3BF5714/test.d.o and /tmp/.rdmd/rdmd-test.d-801BCEFE1CAD8F2472CC2713A3BF5714/test.d.o
collect2: ld returned 1 exit status
----
Unfortunately I'm not sure whether this is supposed to be an error or not. Obviously D doesn't consider scope for the mangled names of static local variables, but if this is meant to be an error then it should be a compiler error, not linker error.
Comment #2 by luk.wrzosek — 2010-11-17T12:12:50Z
Created attachment 817
Fix for this bug.
Comment #3 by luk.wrzosek — 2010-11-18T15:08:29Z
Created attachment 819
Once again fix added and marked (patch)
(In reply to comment #6)
> I just got bit by this. Any new thoughts?
It's not too hard to fix, since static variables don't really need unique mangled names. It's also not terribly high-priority, with all the wrong-code and accepts-invalid bugs that are open.
Comment #8 by code — 2013-12-31T08:59:38Z
You could probably obtain a unique id for each anonymous scope.
Comment #9 by pro.mathias.lang — 2015-07-26T14:41:06Z
(In reply to Mathias LANG from comment #9)
> Nowadays it gives the following error:
>
> test.d(14): Error: declaration test.func.j is already defined in another
> scope in func
>
> Since https://issues.dlang.org/show_bug.cgi?id=11720 was 'fixed'.
So now, the test case won't cause link-failure. Updated summary and keywords.
If the static variables are in extern(D) function, they can have unique mangled names.
But, if they're not in extern(D) function, or does not have extenr(D) linkage, they might conflict. For example:
extern(C++) foo()
{
{ extern(C++) static int var; pragma(msg, var.mangleof); }
{ extern(C++) static int var; pragma(msg, var.mangleof); } // disallowed?
}
Comment #11 by k.hara.pg — 2015-07-27T13:16:15Z
*** Issue 11720 has been marked as a duplicate of this issue. ***
Comment #12 by dlang-bot — 2020-06-07T23:02:46Z
@NilsLankila created dlang/dmd pull request #11250 "fix issue 3031 - static var and func declaration conflict in different sub scope" fixing this issue:
- fix issue 3031 - static var and func declaration conflict in different sub scope
The approach of the fix is based on the observation that the fundamental (and only) problem was that allowing such declarations would produce same mangling.
Now when the insertion in the scope fails, and if the declaration was static, its name is changed and a **local** alias declaration, taking the previous name, is generated.
This works because:
- the static declaration, the one that needs a unique mangle, has now an unique identifier.
- the code following the declaration use the new alias.
https://github.com/dlang/dmd/pull/11250
Comment #13 by moonlightsentinel — 2021-04-17T18:04:57Z