Bug 8940 – Able to modify const/immutable with passing to a templated function by `ref`
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-02T08:17:00Z
Last change time
2012-11-10T18:51:36Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
verylonglogin.reg
Comments
Comment #0 by verylonglogin.reg — 2012-11-02T08:17:31Z
---
const int n; // or `immutable`
static this() { n = 3; }
void f(T)(ref int val)
{
assert(val == 3);
++val;
}
static assert(!__traits(compiles, f!void(n))); // fails
---
As a result of the fact `f!void(n)` compiles:
---
void main()
{
assert(n == 3);
f!void(n);
assert(n == 3); // may pass as compiler caches comparison result
assert(n != 4); // may pass but likely will fail
}
---
This also leads to ability to modify const/immutable data accidentally because of incorrect overload selection:
---
void g(A...)(A args) { }
void g(A...)(ref A args) { assert(0); }
void main()
{
g(1); // ok
g(n); // triggers `assert(0)`
}
---
Comment #1 by verylonglogin.reg — 2012-11-02T08:22:48Z
May be connected with
Issue 8939 - ICE on passing by ref statically initialized const/immutable variable
Comment #2 by github-bugzilla — 2012-11-03T05:09:50Z