Bug 3031 – scoped static var conflicts

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-05-27T08:42:26Z
Last change time
2021-04-17T18:04:57Z
Keywords
bounty, pull
Assigned to
No Owner
Creator
david
See also
https://issues.dlang.org/show_bug.cgi?id=13617

Attachments

IDFilenameSummaryContent-TypeSize
817static-variables.diffFix for this bug.text/plain3403
819static-variables.diffOnce again fix added and marked (patch)text/plain3403

Comments

Comment #0 by davidl — 2009-05-27T08:42:26Z
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)
Comment #4 by bugzilla — 2012-02-18T13:37:50Z
Comment #5 by yebblies — 2012-07-08T05:15:27Z
Issue 5655 is the same sort of thing for lambdas.
Comment #6 by jlquinn — 2013-11-21T22:18:21Z
I just got bit by this. Any new thoughts?
Comment #7 by yebblies — 2013-11-22T00:13:50Z
(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
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'. I'll make it depend on https://issues.dlang.org/show_bug.cgi?id=14831, because the issue doesn't explicitly say which fix was expected. There's also a 20$ bounty on this, so if you guys feel the error message fixes it, feel free to send Kenji (https://github.com/D-Programming-Language/dmd/pull/2947) his reward ;)
Comment #10 by k.hara.pg — 2015-07-27T13:11:38Z
(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
Fixed in 2.095.1