[Issue 3534] New: const data can be modified by passing through ref function parameter

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 20 10:58:34 PST 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 at puremagic.com
        ReportedBy: smjg at iname.com
            Blocks: 2573


--- Comment #0 from Stewart Gordon <smjg at 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: -------


More information about the Digitalmars-d-bugs mailing list