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 18439 Fix issue with static foreach not working with scoped @nogc. #9922

Merged
merged 1 commit into from May 31, 2019
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
Fix Issue 18439 Fix issue with static foreach not working with scoped
  • Loading branch information
look-at-me committed May 31, 2019
commit 0a65ef981578e263a380f6df4668572dce0a52d6
14 changes: 12 additions & 2 deletions src/dmd/cond.d
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,18 @@ extern (C++) final class StaticForeach : RootObject
* sc = The current scope.
*/

private void lowerNonArrayAggregate(Scope* sc)
private void lowerNonArrayAggregate(Scope* scInput)
{
Scope* sc = scInput;

if (sc.stc & STC.nogc)
{
// concating `~=` an array is only done at compile time for `static foreach` range
// remove @nogc to avoid a compile error in this case
sc = scInput.copy();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use push, not copy

sc.stc &= ~STC.nogc;
}

auto nvars = aggrfe ? aggrfe.parameters.dim : 1;
auto aloc = aggrfe ? aggrfe.aggr.loc : rangefe.lwr.loc;
// We need three sets of foreach loop variables because the
Expand Down Expand Up @@ -373,7 +383,7 @@ extern (C++) final class StaticForeach : RootObject
aggrfe ? aggrfe._body : rangefe._body,
aggrfe ? aggrfe.endloc : rangefe.endloc);
rangefe = null;
lowerArrayAggregate(sc); // finally, turn generated array into expression tuple
lowerArrayAggregate(scInput); // finally, turn generated array into expression tuple
}

/*****************************************
Expand Down
10 changes: 10 additions & 0 deletions test/compilable/b18439.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://issues.dlang.org/show_bug.cgi?id=18439
// Internally `static foreach` builds an array using `~=` for ranges which uses the GC
// this would cause a compiler error when @nogc is used, even though the code is only run at compile time

// error specifically occurs when @nogc is applied to a scope
@nogc:

void test() {
static foreach(i; 0 .. 1) {}
}