Thread overview
[Issue 8098] New: Inner class method can modify outer's members regardless of constancy
May 15, 2012
Stewart Gordon
May 15, 2012
Stewart Gordon
May 24, 2012
Kenji Hara
May 25, 2012
Walter Bright
May 15, 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@puremagic.com
        ReportedBy: smjg@iname.com
            Blocks: 2573


--- Comment #0 from Stewart Gordon <smjg@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: -------
May 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8098



--- Comment #1 from Stewart Gordon <smjg@iname.com> 2012-05-15 07:47:50 PDT ---
An inner class holds a hidden member that is a reference to the object of the outer class to which it belongs.  By transitivity, if inner is const or immutable, then outer must be likewise from inner's point of view.

This implies that an immutable inner can only belong to an immutable outer, and a const inner can belong to a mutable, const or immutable outer, but inner will always view the outer as const.

At the moment there doesn't seem to be a way to explicitly set the constancy of Inner.outer.  But if there were, it would enable a mutable inner to belong to an outer of any constancy while ensuring const-safety (though the outer class might not be able to hold a reference to the inner object).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8098


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
           Platform|x86                         |All
         OS/Version|Windows                     |All


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-05-24 08:20:46 PDT ---
https://github.com/D-Programming-Language/dmd/pull/964

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8098



--- Comment #3 from github-bugzilla@puremagic.com 2012-05-24 21:13:49 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/de20f7f9e50c468fe7758604058d0af6bc7f36aa
fix Issue 8098 - Inner class method can modify outer's members regardless of
constancy

https://github.com/D-Programming-Language/dmd/commit/db915a16aba1db00a5ea59f96ad0b0deea7152b5 Merge pull request #964 from 9rnsr/fix8098

Issue 8098 - Inner class method can modify outer's members regardless of constancy

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8098


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


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