www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13336] New: auto ref return deduced to be ref despite return


          Issue ID: 13336
           Summary: auto ref return deduced to be ref despite return value
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: peter.alexander.au gmail.com

int sx;
double sy;

auto ref foo()
  if (1)
    return sx;
  return sy;

foo is determined to return a 'ref double', even though sx is not a double. It
is cast to a double and a ref to the temporary is returned.

Expected behaviour: When common-type coercions are involved, auto ref should
deduce to be non-ref otherwise the code becomes unsafe and surprising.

The spec doesn't say anything about this, it just says "The lexically first
ReturnStatement determines the ref-ness of a function".

Aug 19 2014