[Issue 23747] 'auto ref' function return signature does not flag escaping a reference to local variable

2023-02-28 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=23747

--- Comment #3 from RazvanN  ---
(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());
pragma(msg, typeof());
pragma(msg, typeof());
pragma(msg, typeof());
pragma(msg, typeof());

--


[Issue 23747] 'auto ref' function return signature does not flag escaping a reference to local variable

2023-02-27 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=23747

--- Comment #2 from Mike S  ---
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)

--


[Issue 23747] 'auto ref' function return signature does not flag escaping a reference to local variable

2023-02-27 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=23747

RazvanN  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 CC||razvan.nitu1...@gmail.com
 Resolution|--- |INVALID

--- Comment #1 from RazvanN  ---
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()); -> int function() ref
pragma(msg, typeof()); -> 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".

--


[Issue 23747] 'auto ref' function return signature does not flag escaping a reference to local variable

2023-02-27 Thread d-bugmail--- via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=23747

Iain Buclaw  changed:

   What|Removed |Added

   Priority|P1  |P3

--