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

Fix issue 22306 - scope array variable should be stack allocated #14562

Merged
merged 2 commits into from
Oct 18, 2022
Merged
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
26 changes: 26 additions & 0 deletions changelog/scope-array-on-stack.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Array literals assigned to `scope` array variables are now allocated on the stack

Formerly, they were always allocated with the Garbage Collector, making it unavailable in `@nogc` or `-betterC` code.
This led to frequent use of the following workaround:

---
void main() @nogc
{
int[3] buffer = [10, 20, 30];
int[] arr = buffer[];
}
---

This can now be written in a single line:

---
void main() @nogc
{
scope int[] arr = [10, 20, 30];
}
---

This only applies to array elements that are Plain Old Data, proper destruction cannot be ensured when the array is not managed by the GC.

Note: checking that a `scope` variable actually doesn't escape is only done in `@safe` code.
In a `@system` function, it's your own risk when you corrupt memory by returning a `scope int[]`.
6 changes: 6 additions & 0 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,12 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
if (f.tookAddressOf)
f.tookAddressOf--;
}
else if (auto ale = ex.isArrayLiteralExp())
{
// or an array literal assigned to a `scope` variable
if (!dsym.type.nextOf().needsDestruction())
ale.onstack = true;
}
}

Expression exp = ei.exp;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/e2ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -3868,7 +3868,7 @@ elem* toElem(Expression e, IRState *irs)
elem *e;
if (dim > 0)
{
if (tb.ty == Tsarray ||
if (ale.onstack || tb.ty == Tsarray ||
irs.Cfile && tb.ty == Tpointer)
{
Symbol *stmp = null;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -2949,7 +2949,7 @@ extern (C++) final class ArrayLiteralExp : Expression

Expressions* elements;
OwnedBy ownedByCtfe = OwnedBy.code;

bool onstack = false;

extern (D) this(const ref Loc loc, Type type, Expressions* elements)
{
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class ArrayLiteralExp final : public Expression
Expression *basis;
Expressions *elements;
OwnedBy ownedByCtfe;
bool onstack;

static ArrayLiteralExp *create(const Loc &loc, Expressions *elements);
static void emplace(UnionExp *pue, const Loc &loc, Expressions *elements);
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -6701,7 +6701,7 @@ struct UnionExp final
char complexexp[80LLU];
char symoffexp[72LLU];
char stringexp[60LLU];
char arrayliteralexp[57LLU];
char arrayliteralexp[58LLU];
char assocarrayliteralexp[57LLU];
char structliteralexp[95LLU];
char compoundliteralexp[48LLU];
Expand Down Expand Up @@ -6896,6 +6896,7 @@ class ArrayLiteralExp final : public Expression
Expression* basis;
Array<Expression* >* elements;
OwnedBy ownedByCtfe;
bool onstack;
static ArrayLiteralExp* create(const Loc& loc, Array<Expression* >* elements);
static void emplace(UnionExp* pue, const Loc& loc, Array<Expression* >* elements);
ArrayLiteralExp* syntaxCopy() override;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/nogc.d
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public:

override void visit(ArrayLiteralExp e)
{
if (e.type.ty != Tarray || !e.elements || !e.elements.dim)
if (e.type.ty != Tarray || !e.elements || !e.elements.dim || e.onstack)
return;
if (f.setGC())
{
Expand Down
8 changes: 8 additions & 0 deletions compiler/test/runnable/test20734.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ extern(C) int main() nothrow @nogc @safe
takeScopeSlice([ S(1), S(2) ]); // @nogc => no GC allocation
(() @trusted { assert(numDtor == 2); })(); // stack-allocated array literal properly destructed
assert23100([]);

// https://issues.dlang.org/show_bug.cgi?id=22306
// scope array variable should be stack allocated
scope int[] sa = [10, 20];
assert(sa[0] == 10);
assert(sa[1] == 20);
assert(sa.length == 2);

return 0;
}

Expand Down