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
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/02c301feaafebe754a5298b60394921e32938148 Supplemental fix for Issue 8940 - Able to modify const/immutable with passing to a templated function by `ref` splitUnitsFromHNSecs accepts ref long, so it should not be called with immutable long. But' it has been accidentally accepted by issue 8940. https://github.com/D-Programming-Language/phobos/commit/941b46a0cc12ba6f5633c86f06f3acf3fd448e72 Merge pull request #921 from 9rnsr/fix8940 Supplemental fix for Issue 8940 - Able to modify const/immutable with passing to a templated function by `ref`
Comment #3 by k.hara.pg — 2012-11-03T05:17:39Z
Comment #4 by github-bugzilla — 2012-11-10T11:22:42Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/33a4b46947067defa5800badf5e9ff2c500320e7 fix Issue 8940 - Able to modify const/immutable with passing to a templated function by `ref`