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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 2 08:17:32 PDT 2012


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 at puremagic.com
        ReportedBy: verylonglogin.reg at gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2012-11-02 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: -------


More information about the Digitalmars-d-bugs mailing list