[Issue 8098] New: Inner class method can modify outer's members regardless of constancy

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 15 07:28:38 PDT 2012


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

           Summary: Inner class method can modify outer's members
                    regardless of constancy
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: smjg at iname.com
            Blocks: 2573


--- Comment #0 from Stewart Gordon <smjg at iname.com> 2012-05-15 07:30:06 PDT ---
class Outer {
    int i = 6;

    class Inner {
        int y=0;

        int foo() const {
            pragma(msg, "this.outer: " ~ typeof(this.outer).stringof);
            pragma(msg, "i:          " ~ typeof(i).stringof);
            return ++i;
        }
    }
    Inner inner;
    this() {
        inner = new Inner;
    }
} 

void main() {
    const(Outer) x = new Outer;
    pragma(msg, "x:          " ~ typeof(x).stringof);
    pragma(msg, "x.inner:    " ~ typeof(x.inner).stringof);
    x.inner.foo();
    writeln(x.i);
}
----------
C:\Users\Stewart\Documents\Programming\D\Tests>dmd inner_const.d
this.outer: const(Outer)
i:          const(int)
x:          const(Outer)
x.inner:    const(Inner)

C:\Users\Stewart\Documents\Programming\D\Tests>inner_const
7
----------
(DMD 2.059 Win32)

x is a const reference.  By transitivity, x.inner is.  So far, so good.

Outer.Inner.foo is a const method.  The call fails if it isn't.  So far, so
good.

>From foo's point of view, this.outer and i are reported as const.  So far, so
good.

But despite i being const, it allows it to be modified!

Changing the declaration of x to

    const(Outer) x = new const(Outer);
    const(Outer) x = new immutable(Outer);
    immutable(Outer) x = new immutable(Outer);

makes no difference to the bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list