Thread overview
[Issue 3534] New: const data can be modified by passing through ref function parameter
Nov 20, 2009
Stewart Gordon
Sep 17, 2010
Stewart Gordon
[Issue 3534] const/immutable data can be modified by passing through ref function parameter
Jan 27, 2011
yebblies
November 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3534

           Summary: const data can be modified by passing through ref
                    function parameter
           Product: D
           Version: 2.036
          Platform: Other
        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> 2009-11-20 10:58:34 PST ---
Based on a newsgroup post by Tomek SowiƱski.  Both these testcases compile without error:

----------
import std.stdio;

const int a;

void foo(ref int i) {
    i = 4;
}

void foo() {
    foo(a);
}

void main() {
    writefln("%d", a);
    foo();
    writefln("%d", a);
}
----------
The bug disappears if a is explicitly initialised.

----------
import std.stdio;

const int value;

void changeConst() {
    doChange(value);
}

void doChange(ref int var) {
    var = 42;
}

void main() {
    writefln("%d", value);
    changeConst();
    writefln("%d", value);
}
----------
The bug disappears if a is explicitly initialised.

----------
import std.stdio;

class Class {
    int value;

    void changeConst() const {
        doChange(value);
    }

    void doChange(ref int var) const {
        var = 42;
    }
}

void main() {
    Class obj = new Class;
    writefln("%d", obj.value);
    obj.changeConst();
    writefln("%d", obj.value);
}

----------
The bug also bits if doChange is made static or global and the const attribute removed.  If only the const attribute is removed, the error is correctly diagnosed (though this may be partly due to another bug).

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


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha@yahoo.com


--- Comment #1 from Stewart Gordon <smjg@iname.com> 2010-09-17 05:17:43 PDT ---
*** Issue 4877 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3534


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #2 from yebblies <yebblies@gmail.com> 2011-01-26 18:27:29 PST ---
*** This issue has been marked as a duplicate of issue 5493 ***

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