[Issue 1849] New: with() should cause shadowing errors if you use a member that's shadowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 18 00:19:32 PST 2008


http://d.puremagic.com/issues/show_bug.cgi?id=1849

           Summary: with() should cause shadowing errors if you use a member
                    that's shadowed
           Product: D
           Version: 1.025
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: wbaxter at gmail.com


Sorry for the horrible summary.  Kudos to anyone who can think of a better one.

Issue is this:
----
module shadow2;

struct Foo {
    static Foo opCall(int numRows, int numCols) {
        Foo R; with (R) {
            numRows_ = numRows;
            numCols_ = numRows;
        } return R;
    }

    int numRows() { return numRows_; }
    int numCols() { return numCols_; }

private:
    int numRows_ = -1;
    int numCols_ = -1;
}


void main()
{
    auto M = Foo(10,20);
    assert(M.numRows == -1, "numRows Shouldn't be -1, actually");
    assert(M.numCols == -1, "numCols Shouldn't be -1, actually");
    assert(M.numRows == 10, "numRows Should be 10 but it isn't");
    assert(M.numCols == 20, "numCols Should be 20 but it isn't");
}
----

What happens is the with() there in the static opCall causes R.numRows() to
shadow the parameter named 'numRows'.  So you're actually just setting numRows_
to what it already was.

This is pretty evil that this happens silently, so I think it should generate a
shadowing error.  But the tricky part is that error should only be generated if
you try to *use* a shadowed variable.  If errors were triggered even when no
shadowed variable was used then with() could become practically useless because
such many such "potential shadowings".


-- 



More information about the Digitalmars-d-bugs mailing list