D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5297 - The lookup order of recursive with statements is undefined.
Summary: The lookup order of recursive with statements is undefined.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2010-12-01 05:27 UTC by Bernard Helyer
Modified: 2021-01-24 06:54 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Bernard Helyer 2010-12-01 05:27:29 UTC
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 Simen Kjaeraas 2010-12-01 05:44:53 UTC
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 bearophile_hugs 2010-12-01 12:39:36 UTC
(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 Simen Kjaeraas 2010-12-01 14:59:32 UTC
(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 Walter Bright 2012-01-20 11:29:23 UTC
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.
Comment 5 mhh 2021-01-24 06:54:48 UTC
Doesn't seem to be a problem anymore