www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20416] New: [Regression 2.073.2] compiler complains about

https://issues.dlang.org/show_bug.cgi?id=20416

          Issue ID: 20416
           Summary: [Regression 2.073.2] compiler complains about escaping
                    reference in certain cases
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: rejects-valid, safe
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

In some cases, when a function returns the result of a ref function by value,
the compiler complains that it's escaping a local reference.

An example case:

struct S
{
   string x;
}

ref S foo(return ref S s) { return s; }

S bar()
{
   S result;
   return foo(result);
}

Error: returning foo(result) escapes a reference to local variable result

Note the return attribute in foo is significant. Without that it succeeds. Also
significant is that S contains a string. If you replace it with an int, it
works. Probably has something to do with the indirection in the string.

Clearly, we aren't returning by ref from bar, but the compiler fails to see
this.

Seems to be a regression for 2.073 (where dip1000 was introduced). With
-dip1000 switch, it still fails.

Workaround is to call foo, and then return result directly, which oddly shows
no complaint even though it's equivalent.

--
Nov 24 2019