Bug 9521 – Missed with() anti-hijacking

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-02-16T07:25:00Z
Last change time
2013-11-26T05:33:22Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-02-16T07:25:57Z
void foo() {} enum E { foo } void main() { E e; final switch (e) with (E) { foo: foo(); break; } } DMD 2.062beta gives: temp.d(6): Error: function expected before (), not cast(E)0 of type E temp.d(5): Error: enum member foo not represented in final switch But I expect only one different error message, similar to: temp.d(5): Error: with symbol temp.E.foo is shadowing global symbol temp.foo Similar to: struct Foo { int x; } void main() { int x; Foo f; with (f) { x++; } } temp.d(6): Error: with symbol temp.Foo.x is shadowing local symbol temp.main.x
Comment #1 by yebblies — 2013-11-22T22:02:07Z
void foo() is _not_ a local symbol. An analogous version of the second example would be: struct Foo { int x; } int x; void main() { Foo f; with (f) { x++; } } Which does _not give an error.
Comment #2 by bearophile_hugs — 2013-11-23T05:08:45Z
(In reply to comment #1) Sorry for my first example, that is badly written. > void foo() is _not_ a local symbol. > > An analogous version of the second example would be: > > struct Foo { int x; } > int x; > void main() { > Foo f; > with (f) { > x++; > } > } > > Which does _not give an error. Right, currently with() is not designed to have anti-hijacking of module-level identifiers. So this issue is not a bug. So is it a good idea to modify the anti-hijacking of with() and produce an error even when you shade a global variable like that module-level x with the field x of Foo?
Comment #3 by yebblies — 2013-11-23T05:13:40Z
I don't know. If we change with to count global (and class member) shadowing, we should probably change the normal variable shadowing too.
Comment #4 by bearophile_hugs — 2013-11-26T05:27:34Z
(In reply to comment #3) > I don't know. If we change with to count global (and class member) shadowing, > we should probably change the normal variable shadowing too. There is an ongoing long thread on a very related topic (the thread starts with something unrelated): http://forum.dlang.org/thread/[email protected] There is no consensus regarding what to do. I think shadowing global variables with a with() isn't a very good thing. But Andrei seems to not like the idea of turning an error the shadowing of locals with module-level variables. So I have no final answer yet.
Comment #5 by yebblies — 2013-11-26T05:33:22Z
(In reply to comment #4) > (In reply to comment #3) > > I don't know. If we change with to count global (and class member) shadowing, > > we should probably change the normal variable shadowing too. > > There is an ongoing long thread on a very related topic (the thread starts with > something unrelated): > http://forum.dlang.org/thread/[email protected] > > There is no consensus regarding what to do. I think shadowing global variables > with a with() isn't a very good thing. But Andrei seems to not like the idea of > turning an error the shadowing of locals with module-level variables. So I have > no final answer yet. I don't see it happening. Put it in your dlint list.