Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3060,16 +3060,25 @@ else
{
/* Determine "refness" of function return:
* if it's an lvalue, return by ref, else return by value
* https://dlang.org/spec/function.html#auto-ref-functions
*/

void turnOffRef()
{
tf.isref = false; // return by value
tf.isreturn = false; // ignore 'return' attribute, whether explicit or inferred
fd.storage_class &= ~STC.return_;
}

if (rs.exp.isLvalue())
{
/* May return by ref
*/
if (checkReturnEscapeRef(sc, rs.exp, true))
tf.isref = false; // return by value
turnOffRef();
}
else
tf.isref = false; // return by value
turnOffRef();

/* The "refness" is determined by all of return statements.
* This means:
Expand Down
15 changes: 15 additions & 0 deletions test/compilable/test17512.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://issues.dlang.org/show_bug.cgi?id=17512

struct A
{
int _value;

bool _hasValue;

auto ref getOr(int alternativeValue)
{
return _hasValue ? _value : alternativeValue;
}
}

A a;