Bug 3534 – const/immutable data can be modified by passing through ref function parameter

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2009-11-20T10:58:00Z
Last change time
2015-06-09T01:27:05Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
smjg
Blocks
2573

Comments

Comment #0 by smjg — 2009-11-20T10:58:34Z
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).
Comment #1 by smjg — 2010-09-17T05:17:43Z
*** Issue 4877 has been marked as a duplicate of this issue. ***
Comment #2 by yebblies — 2011-01-26T18:27:29Z
*** This issue has been marked as a duplicate of issue 5493 ***