Skip to content

Commit

Permalink
fix Issue 18644 - [dip1000] escape of outer local not detected
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Mar 21, 2018
1 parent 54ab04e commit 51565f3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,19 @@ private void escapeByValue(Expression e, EscapeByResults* er)
if (tf.isreturn)
e.e1.accept(this);
}

/* If it's a nested function that is 'return scope'
*/
if (e.e1.op == TOK.variable)
{
VarExp ve = cast(VarExp)e.e1;
FuncDeclaration fd = ve.var.isFuncDeclaration();
if (fd && fd.isNested())
{
if (tf.isreturn && tf.isscope)
er.byexp.push(e);
}
}
}
}

Expand Down Expand Up @@ -1480,6 +1493,19 @@ private void escapeByRef(Expression e, EscapeByResults* er)
{
escapeByValue(e.e1, er);
}

/* If it's a nested function that is 'return ref'
*/
if (e.e1.op == TOK.variable)
{
VarExp ve = cast(VarExp)e.e1;
FuncDeclaration fd = ve.var.isFuncDeclaration();
if (fd && fd.isNested())
{
if (tf.isreturn)
er.byexp.push(e);
}
}
}
else
er.byexp.push(e);
Expand Down
25 changes: 25 additions & 0 deletions test/fail_compilation/test18644.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* REQUIRED_ARGS: -dip1000
TEST_OUTPUT:
---
fail_compilation/test18644.d(15): Error: storing reference to stack allocated value returned by `foo()` into allocated memory causes it to escape
fail_compilation/test18644.d(16): Error: escaping reference to stack allocated value returned by `foo()`
fail_compilation/test18644.d(22): Error: escaping reference to stack allocated value returned by `foo()`
---
*/

// https://issues.dlang.org/show_bug.cgi?id=18644

@safe int* test1() {
int i;
int* foo() { return &i; }
int*[] b = [foo()];
return foo();
}

@safe ref int test2() {
int i;
ref int foo() { return i; }
return foo();
}


0 comments on commit 51565f3

Please sign in to comment.