www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8940] New: Able to modify const/immutable with passing to a templated function by `ref`

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940

           Summary: Able to modify const/immutable with passing to a
                    templated function by `ref`
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: verylonglogin.reg gmail.com



18:17:31 MSK ---
---
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)`
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940




18:22:48 MSK ---
May be connected with
Issue 8939 - ICE on passing by ref statically initialized const/immutable
variable

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940




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


Supplemental fix for Issue 8940 - Able to modify const/immutable with passing
to a templated function by `ref`

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



https://github.com/D-Programming-Language/dmd/pull/1252

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940




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`

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 10 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8940


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 10 2012