Bug 5084 – Static code does not ignore instance names during name lookup
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-10-20T03:53:00Z
Last change time
2013-01-26T17:52:46Z
Assigned to
nobody
Creator
ah08010-d
Comments
Comment #0 by ah08010-d — 2010-10-20T03:53:30Z
In the code
==========
module scratch;
import std.stdio;
int[ 3 ] x;
struct S {
int[ 2 ] x;
static void foo() {
writeln( x[2] );
}
}
=========
I would expect that the static method performed static name lookup. In this case,
that would resolve the x[] reference to the variable at module scope.
Instead, the reference is apparently resolved to the member, despite the member being an illegal reference. If the [2], which generates an "index out of bounds" error, is replaced with [1], then the compiler proceeds to issue a "this required for reference to member" error.
Obviously, .x would work in this case, but I think this may be a general bug. Alternatively, could someone provide a pointer to the rules for name lookup?
Comment #1 by andrej.mitrovich — 2013-01-26T17:52:46Z
That would mean that an imported module which has a module-scoped variable or property function with the same name as a non-static field could hijack that field instead of giving you an error. It would be dangerous to allow such behavior.
You can always use the dot prefix to lookup module-scoped variables, via writeln(.x[2]).