Thread overview
[Issue 23747] 'auto ref' function return signature does not flag escaping a reference to local variable
Feb 27, 2023
Iain Buclaw
Feb 27, 2023
RazvanN
Feb 27, 2023
Mike S
Feb 28, 2023
RazvanN
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23747

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23747

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |razvan.nitu1305@gmail.com
         Resolution|---                         |INVALID

--- Comment #1 from RazvanN <razvan.nitu1305@gmail.com> ---
This bug report is invalid.

All of the `auto ref` functions work as expected. `Auto ref` does not mean "ref + type deduction", but rather "deduce if a function may return by ref + type deduction". In all the situations presented, the compiler sees that it returns a reference to the stack and therefore chooses to drop the ref and return by value.

You can add this code and see the types of the functions:

pragma(msg, typeof(&Func1)); -> int function() ref
pragma(msg, typeof(&Func2)); -> int function() pure nothrow @nogc @safe

I guess the spec indeed is misleading by stating that "they become ref functions if all return expressions are lvalues". Actually, its more like "they become ref functions, if possible".

--
February 27, 2023
https://issues.dlang.org/show_bug.cgi?id=23747

--- Comment #2 from Mike S <Michael.Shah@tufts.edu> ---
Ah noted, that is perhaps my mistake then. Looking at the specification again with this in mind, I now see how 'auto ref' is deducing the 'refness' of a function.

This means 'auto ref' snd 'ref auto' have different meanings then to be clear?

- 'auto ref' deducing if it is possible to return by reference, and 'ref auto' explicitly indicating return by reference, and deduce the type (e.g. auto may be a double if possible return values are 1.0 and 1)

--
February 28, 2023
https://issues.dlang.org/show_bug.cgi?id=23747

--- Comment #3 from RazvanN <razvan.nitu1305@gmail.com> ---
(In reply to Mike S from comment #2)
> Ah noted, that is perhaps my mistake then. Looking at the specification again with this in mind, I now see how 'auto ref' is deducing the 'refness' of a function.
> 
> This means 'auto ref' snd 'ref auto' have different meanings then to be clear?
> 
> - 'auto ref' deducing if it is possible to return by reference, and 'ref auto' explicitly indicating return by reference, and deduce the type (e.g. auto may be a double if possible return values are 1.0 and 1)

No, `auto ref` or `ref auto` is the same thing. If you want to have a ref return type that is deduced you can simply omit the return type. The following example showcases that `auto ref` and `ref auto` are the same thing:

// dmd -c test.d

auto ref fun()
{
    int x;
    return x;
}

ref auto fun2()
{
    int x;
    return x;
}

int t;

auto ref fun3()
{
    return t;
}

ref auto fun4()
{
    return t;
}

ref fun5()
{
    return t;
}

ref fun6()
{
    int x;
    return x;     // error
}


pragma(msg, typeof(&fun));
pragma(msg, typeof(&fun2));
pragma(msg, typeof(&fun3));
pragma(msg, typeof(&fun4));
pragma(msg, typeof(&fun5));

--