Skip to content

Commit

Permalink
fix Issue 12932 - Support @nogc for immediately iterated array literal
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jun 16, 2014
1 parent c840b0d commit dfb44a6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/statement.c
Expand Up @@ -1967,7 +1967,19 @@ Statement *ForeachStatement::semantic(Scope *sc)
*/
Identifier *id = Lexer::uniqueId("__aggr");
ExpInitializer *ie = new ExpInitializer(loc, new SliceExp(loc, aggr, NULL, NULL));
VarDeclaration *tmp = new VarDeclaration(loc, tab->nextOf()->arrayOf(), id, ie);
VarDeclaration *tmp;
if (aggr->op == TOKarrayliteral &&
!((*arguments)[dim - 1]->storageClass & STCref))
{
ArrayLiteralExp *ale = (ArrayLiteralExp *)aggr;
size_t edim = ale->elements ? ale->elements->dim : 0;
aggr->type = tab->nextOf()->sarrayOf(edim);

// for (T[edim] tmp = a, ...)
tmp = new VarDeclaration(loc, aggr->type, id, ie);
}
else
tmp = new VarDeclaration(loc, tab->nextOf()->arrayOf(), id, ie);
tmp->storage_class |= STCtemp;

Expression *tmp_length = new DotIdExp(loc, new VarExp(loc, tmp), Id::length);
Expand Down
19 changes: 19 additions & 0 deletions test/fail_compilation/fail12932.d
@@ -0,0 +1,19 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail12932.d(11): Error: array literal in @nogc function foo may cause GC allocation
fail_compilation/fail12932.d(15): Error: array literal in @nogc function foo may cause GC allocation
---
*/

int* foo() @nogc
{
foreach (ref e; [1,2,3])
{
}

foreach (ref e; [1,2,3])
{
return &e;
}
}
14 changes: 14 additions & 0 deletions test/runnable/foreach5.d
Expand Up @@ -949,6 +949,19 @@ void test12739() nothrow
foreach (e; s) {}
}

/***************************************/
// 12932

void test12932() @nogc
{
int sum;
foreach (e; [1,2,3])
{
sum += e;
}
assert(sum == 6);
}

/***************************************/

int main()
Expand Down Expand Up @@ -977,6 +990,7 @@ int main()
test11291();
test12103();
test12739();
test12932();

printf("Success\n");
return 0;
Expand Down

0 comments on commit dfb44a6

Please sign in to comment.