Comment #0 by snarwin+bugzilla — 2022-03-17T13:28:48Z
As of DMD 2.099.0, the following program compiles and runs without error:
--- lib.d
int x = 1;
--- main.d
void main()
{
int x = 2;
{
import lib : x;
assert(x == 1);
}
}
---
The language spec [1] states that name lookup proceeds in two phases: first, symbols in the current module and inherited scopes are considered; second, imported symbols are considered. It is not stated explicitly whether selectively-imported symbols are considered in phase 1 or phase 2.
If selectively-imported symbols are considered in phase 1, the above program should yield a compile-time error, because symbols are not permitted to shadow other symbols declared in the same function.
If selectively-imported symbols are considered in phase 2, the above program should yield a runtime error, because the local `x` should hide the imported `x`.
Either way, the observed behavior cannot possibly be correct.
[1] https://dlang.org/spec/module.html#name_lookup
Comment #1 by b2.temp — 2022-03-17T13:44:02Z
isn't x with 2 as value hidden on purpose ?
Comment #2 by b2.temp — 2022-03-17T13:45:53Z
ah I see the problem now, the x that has 2 for value cant be used anymore.
No way to fully qualify it.
Comment #3 by snarwin+bugzilla — 2022-03-17T14:04:07Z
Note that the following similar program does NOT compile:
--- lib.d
int x = 1;
--- main.d
void main()
{
int x = 2;
{
import lib;
alias x = lib.x;
assert(x == 1);
}
}
---
The error given is
---
main.d(6): Error: alias `x` is shadowing variable `main.main.x`
---
Comment #4 by robert.schadek — 2024-12-13T19:21:28Z