Skip to content

Commit

Permalink
Merge pull request #8036 from WalterBright/fix18484
Browse files Browse the repository at this point in the history
fix Issue 18484 - [dip1000] Subtype allows reference to escape with i…
merged-on-behalf-of: Walter Bright <WalterBright@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Mar 16, 2018
2 parents 98a3490 + ff6edd1 commit 03eb2b7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/dmd/escape.d
Expand Up @@ -820,7 +820,12 @@ private void escapeByValue(Expression e, EscapeByResults* er)

override void visit(AddrExp e)
{
escapeByRef(e.e1, er);
/* Taking the address of struct literal is normally not
* allowed, but CTFE can generate one out of a new expression,
* but it'll be placed in static data so no need to check it.
*/
if (e.e1.op != TOK.structLiteral)
escapeByRef(e.e1, er);
}

override void visit(SymOffExp e)
Expand Down Expand Up @@ -1161,6 +1166,19 @@ private void escapeByRef(Expression e, EscapeByResults* er)
}
}

override void visit(StructLiteralExp e)
{
if (e.elements)
{
foreach (ex; *e.elements)
{
if (ex)
ex.accept(this);
}
}
er.byexp.push(e);
}

override void visit(DotVarExp e)
{
Type t1b = e.e1.type.toBasetype();
Expand Down
26 changes: 26 additions & 0 deletions test/fail_compilation/test18484.d
@@ -0,0 +1,26 @@
/* REQUIRED_ARGS: -dip1000
TEST_OUTPUT:
---
fail_compilation/test18484.d(19): Error: returning `x.bar()` escapes a reference to local variable `x`
fail_compilation/test18484.d(24): Error: escaping reference to stack allocated value returned by `S(0)`
---
*/

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

struct S
{
int* bar() return;
int i;
}

int* test1()
{
auto x = S(); return x.bar(); // error
}

int* test2()
{
return S().bar(); // error
}

0 comments on commit 03eb2b7

Please sign in to comment.