Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape.d: fix escapeExp related to allocated memory #16734

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
34 changes: 13 additions & 21 deletions compiler/src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -1659,16 +1659,16 @@ void escapeExp(Expression e, ref scope EscapeByResults er, int deref)

void visitArrayLiteral(ArrayLiteralExp e)
{
Type tb = e.type.toBasetype();
if (tb.isTypeSArray() || tb.isTypeDArray())
// Putting something in an array literal dereferences it (usually caught by `checkNewEscape`)
// unless it's a static array
const bool isDynamic = e.type.toBasetype().isTypeSArray() is null;

if (e.basis)
escapeExp(e.basis, er, deref + isDynamic);
foreach (el; *e.elements)
{
if (e.basis)
escapeExp(e.basis, er, deref);
foreach (el; *e.elements)
{
if (el)
escapeExp(el, er, deref);
}
if (el)
escapeExp(el, er, deref + isDynamic);
}
}

Expand All @@ -1684,7 +1684,7 @@ void escapeExp(Expression e, ref scope EscapeByResults er, int deref)
}
if (deref == -1)
{
er.byExp(e, er.inRetRefTransition > 0); //
er.byExp(e, er.inRetRefTransition > 0);
}
}

Expand All @@ -1696,7 +1696,7 @@ void escapeExp(Expression e, ref scope EscapeByResults er, int deref)
foreach (ex; *e.arguments)
{
if (ex)
escapeExp(ex, er, deref);
escapeExp(ex, er, deref + 1);
}
}
}
Expand Down Expand Up @@ -1725,16 +1725,8 @@ void escapeExp(Expression e, ref scope EscapeByResults er, int deref)

void visitIndex(IndexExp e)
{
Type tb = e.e1.type.toBasetype();

if (tb.isTypeSArray())
{
escapeExp(e.e1, er, deref);
}
else if (tb.isTypeDArray())
{
escapeExp(e.e1, er, deref + 1);
}
const bool isDynamic = e.e1.type.toBasetype().isTypeSArray() is null;
escapeExp(e.e1, er, deref + isDynamic);
}

void visitBin(BinExp e)
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/fail_compilation/fail13902.d
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,22 @@ ref int testEscapeRef2(
/*
TEST_OUTPUT:
---
fail_compilation/fail13902.d(294): Error: returning `[& x]` escapes a reference to local variable `x`
fail_compilation/fail13902.d(295): Error: returning `[& x]` escapes a reference to local variable `x`
---
*/
int*[] testArrayLiteral1() { int x; return [&x]; }

int*[] testArrayLiteral1() { int x; return [&x]; } // not caught in `@system` code
int*[1] testArrayLiteral2() { int x; return [&x]; }

/*
TEST_OUTPUT:
---
fail_compilation/fail13902.d(304): Error: returning `S2(& x)` escapes a reference to local variable `x`
fail_compilation/fail13902.d(305): Error: returning `new S2(& x)` escapes a reference to local variable `x`
---
*/

S2 testStructLiteral1() { int x; return S2(&x); }
S2* testStructLiteral2() { int x; return new S2(&x); }
S2* testStructLiteral2() { int x; return new S2(&x); } // not caught in `@system` code

/*
TEST_OUTPUT:
Expand Down
Loading