www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3534] New: const data can be modified by passing through ref function parameter

reply d-bugmail puremagic.com writes:
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



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: -------
Nov 20 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3534


Stewart Gordon <smjg iname.com> changed:

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



*** 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: -------
Sep 17 2010
prev sibling parent d-bugmail puremagic.com writes:
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



*** 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: -------
Jan 26 2011