View mode: basic / threaded / horizontal-split · Log in · Help
February 19, 2013
[Issue 9537] New: auto ref returns a reference its own stack

          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

--- Comment #0 from Zach the Mystic <> 2013-02-19 10:54:14 PST ---
I'm conservatively marking this 'normal' although it actually seems 'major' to
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

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.
3) 'auto ref' completely defeating its purpose by returning a reference to a
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:
------- You are receiving this mail because: -------
Top | Discussion index | About this forum | D home