Thread overview
[Issue 3555] New: Const function modifies a field when passed a delegate
Nov 28, 2009
Tomasz Sowiński
Dec 12, 2010
Jonathan M Davis
Dec 12, 2010
Walter Bright
November 28, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3555

           Summary: Const function modifies a field when passed a delegate
           Product: D
           Version: 2.036
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: tomeksowi@gmail.com


--- Comment #0 from Tomasz Sowiński <tomeksowi@gmail.com> 2009-11-28 04:43:30 PST ---
I believe this should not compile:

class A
{ public:
    int pole;

    void funkcja(void delegate(int) dg) const
    {
        dg(3);
    }
}

void main()
{
    A a = new A;

    void zmien(int w)
    {
        a.pole = w;
    }

    a.funkcja(&zmien);  // pole changed by calling const funkcja
    assert (a.pole == 3);
}

Looks very similar to bug 3534.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3555


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-12-12 10:56:37 PST ---
This is not a bug. And the reason is simple: all marking a function as const does is make its this reference const. So, funkcja becomes something like

void funkcja(const A this, void delegate(int) dg)
{
    dg(3);
}


The delegate has a separate reference to the object that this points to. Even if you have a pointer or reference to const, it's perfectly legal to alter what they refer to through another pointer or reference - const just protects what is const. If you want to _guarantee_ that a const function cannot in any way alter any aspect of the class or struct that its a part if, that function must be both const and strongly pure. So, it'll have to be marked const and pure, and all of its parameters must either be immutable or implicitly castable to immutable (so, they must either be primitive types or they must be structs that don't hold non-immutable pointers or references). Otherwise, it's conceivable that you'll alter the value of the object that this points to through some indirection. In most cases, you obviously won't but it _is_ possible.

If the object referred to by this were immutable, then that wouldn't be a problem, because then _no_ pointer or reference could alter it, but as long as the item referred to be const is actually mutable, then other pointers or references which aren't pointers or references to const can alter that item.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3555


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2010-12-12 13:30:27 PST ---
Jonathan's remarks are correct. Not a bug.

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