Bug 5297 – The lookup order of recursive with statements is undefined.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-12-01T05:27:29Z
Last change time
2021-01-24T06:54:48Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
Bernard Helyer
Comments
Comment #0 by b.helyer — 2010-12-01T05:27:29Z
In implementing SDC I find myself asking these questions, and I wish to try and put and end to DMD being the sole definer of semantics; just letting you know the reason for this and subsequent bug reports.
Given a series of with statements:
with (a) with (b) with (c) {
d();
}
If all three objects have a method d(), then c gets called. Obviously the with statements are considered in a LIFO order, and the sentence
http://www.digitalmars.com/d/2.0/statement.html#WithStatement
"Use of with object symbols that shadow local symbols with the same identifier are not allowed. "
does not apply here. Neither is documented behaviour, and the only to figure out how D behaves is to run DMD.
Comment #1 by simen.kjaras — 2010-12-01T05:44:53Z
This is a compiler bug. Symbols used in with should shadow symbols from other with statements.
You're right however, that the spec should state this.
Comment #2 by bearophile_hugs — 2010-12-01T12:39:36Z
(In reply to comment #1)
> Symbols used in with should shadow symbols from other
> with statements.
Do you mean "shouldn't"?
This shadowing is not detected, and I think the compiler has to flag this is as a compile error (just like it does with normal variables in the function stack frame):
struct X { int a; }
struct Y { int a; }
void main() {
X x;
Y y;
with (x) {
a = 2;
with (y) {
a = 1;
}
}
assert(x.a == 2);
}
Comment #3 by simen.kjaras — 2010-12-01T14:59:32Z
(In reply to comment #2)
> (In reply to comment #1)
> > Symbols used in with should shadow symbols from other
> > with statements.
>
> Do you mean "shouldn't"?
Yes and no. It shouldn't silently do so, but it should detect that that is what is happening, and cry out loudly.
Comment #4 by bugzilla — 2012-01-20T11:29:23Z
The spec looks correct to me. The lookup order is not undefined, each successive with introduces a new scope which overrides previous scopes.
As for the shadowing, that is a bug in the compiler.