July 25, 2019
https://issues.dlang.org/show_bug.cgi?id=20084

          Issue ID: 20084
           Summary: return by auto ref unsafe - and different code for
                    inout ref and ref.
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: ali.akhtarzada@gmail.com

Original issue opened here: https://github.com/ldc-developers/ldc/issues/3115 but it seems maybe a frontend thing?

A function returning by auto ref seems to return by value if it in turn calls an ref T function, where as it seems to return by ref if it calls an inout ref T.

I assume this may have something to do with inout ref being inferred as a return? And so inout doesn't generate a value? But why does ref T return by ref? And how can one make this generic code safe:

auto ref get(Range)(Range range) {
    return range.front;
}

If you take the following code and look at the output you'll see what's happening:

struct W(T) {
    T value;
    ref inout(T) inoutFront() inout { return value; }
    ref T front() { return value; }
}

auto ref inoutGet(T)(W!T value) {
    return value.inoutFront;
}

auto ref get(T)(W!T value) {
    return value.front;
}

extern(C) void main() {
    int inoutS = W!int(4).inoutGet;
    int s = W!int(6).get;
}

--