Skip to content
Closed
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
23 changes: 15 additions & 8 deletions src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,10 @@ bool checkAssignEscape(Scope* sc, Expression e, bool gag)
(va.storage_class & (STC.ref_ | STC.out_) || va.type.toBasetype().ty == Tclass);
if (log && vaIsRef) printf("va is ref `%s`\n", va.toChars());

/* Determine if va is the first parameter, through which other 'return' parameters
/* Determine if va is the first non-return parameter, through which other 'return' parameters
* can be assigned.
*/
bool isFirstRef()
bool isReturnDest()
{
if (!vaIsRef)
return false;
Expand All @@ -613,13 +613,20 @@ bool checkAssignEscape(Scope* sc, Expression e, bool gag)
return false;
if (va == fd.vthis)
return true;
if (fd.parameters && fd.parameters.dim && (*fd.parameters)[0] == va)
return true;
if (fd.parameters)
{
foreach (i, v; *fd.parameters) {
if (v == va)
return true;
if (!(v.storage_class & STC.return_))
return false;
}
}
}
return false;
}
const bool vaIsFirstRef = isFirstRef();
if (log && vaIsFirstRef) printf("va is first ref `%s`\n", va.toChars());
const bool vaIsReturnDest = isReturnDest();
if (log && vaIsReturnDest) printf("va is first ref `%s`\n", va.toChars());

bool result = false;
foreach (VarDeclaration v; er.byvalue)
Expand All @@ -643,7 +650,7 @@ bool checkAssignEscape(Scope* sc, Expression e, bool gag)
continue;
}

if (vaIsFirstRef &&
if (vaIsReturnDest &&
(v.isScope() || (v.storage_class & STC.maybescope)) &&
!(v.storage_class & STC.return_) &&
v.isParameter() &&
Expand All @@ -659,7 +666,7 @@ bool checkAssignEscape(Scope* sc, Expression e, bool gag)

if (v.isScope())
{
if (vaIsFirstRef && v.isParameter() && v.storage_class & STC.return_)
if (vaIsReturnDest && v.isParameter() && v.storage_class & STC.return_)
{
if (va.isScope())
continue;
Expand Down
13 changes: 12 additions & 1 deletion test/fail_compilation/test19097.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* REQUIRED_ARGS: -dip1000
* TEST_OUTPUT:
---
fail_compilation/test19097.d(35): Error: scope variable `s` may not be returned
fail_compilation/test19097.d(25): Error: scope variable `p` assigned to `r` with longer lifetime
fail_compilation/test19097.d(46): Error: scope variable `s` may not be returned
---
*/

Expand All @@ -14,6 +15,16 @@ void betty(ref scope int* r, return scope int* p)
r = p;
}

void tabby(return scope int* p, ref scope int* r)
{
r = p;
}

void kitty(return scope int* p, int intermediate, ref scope int* r)
{
r = p;
}

void freddy(out scope int* r, return scope int* p)
{
r = p;
Expand Down