[Issue 20084] New: return by auto ref unsafe - and different code for inout ref and ref.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 25 17:49:24 UTC 2019


          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 at puremagic.com
          Reporter: ali.akhtarzada at 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

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

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;


More information about the Digitalmars-d-bugs mailing list