www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9537] New: auto ref returns a reference its own stack

http://d.puremagic.com/issues/show_bug.cgi?id=9537

           Summary: auto ref returns a reference its own stack
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: reachzach gmail.com



PST ---
I'm conservatively marking this 'normal' although it actually seems 'major' to
me.
Monarch Dodra has said that this code compiles:

import std.typecons;
auto ref foo(T)(auto ref T t)
{
    return t[0];
}

void main()
{
    int* p = &foo(tuple(1, 2));
}

Our concern is that p could only be pointing to the vaporized stack at this
point. tuple(1,2) is a rvalue struct type, which means that function 'foo'
should be interpreting its parameter 't' as a value, not a reference. t[0]
therefore is a derived from a value type, which should not be returnable by
ref.

Value parameters should not be returnable by 'ref'. That is an obvious
stack-breaking maneuver. I don't know exactly where the problem is at this
point, or if there are multiple unsafe operations here:

1) auto ref parameter assumed to be a reference when spec says its a value
2) index of a tuple not understood to be derived from a value parameter (i.e.
local)
3) 'auto ref' completely defeating its purpose by returning a reference to a
value
4) some syntactical ambiguity with '&foo(tuple(1,2))' which I'm not aware of
5) taking the address of something returned as a value

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 19 2013