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
            Blocks: 2573

--- Comment #0 from Stewart Gordon <> 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);;
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)

(DMD 2.059 Win32)

x is a const reference.  By transitivity, x.inner is.  So far, so good. is a const method.  The call fails if it isn't.  So far, so

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

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:
------- You are receiving this mail because: -------

Reply via email to