Skip to content

Commit

Permalink
fix Issue 17512 - [REG 2.073] [DIP1000] Error on bad interplay of 'au…
Browse files Browse the repository at this point in the history
…to ref' and 'return' attribute deduction
  • Loading branch information
WalterBright authored and wilzbach committed Mar 11, 2018
1 parent 33bc192 commit 56b770c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/dmd/statementsem.d
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
@@ -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;

0 comments on commit 56b770c

Please sign in to comment.