Skip to content

Commit

Permalink
Issue 12936 - Some more @nogc cases for immediately iterated array li…
Browse files Browse the repository at this point in the history
…teral

Support case#2 in bugzilla.
  • Loading branch information
9rnsr committed Aug 10, 2016
1 parent 40fc716 commit 6a48d5b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -874,15 +874,21 @@ private extern (C++) final class StatementSemanticVisitor : Visitor
* for (T[] tmp = a[], size_t key = tmp.length; key--; )
* { T value = tmp[k]; body }
*/
Identifier id = Identifier.generateId("__r");
auto id = Identifier.generateId("__r");
auto ie = new ExpInitializer(loc, new SliceExp(loc, fs.aggr, null, null));
VarDeclaration tmp;
if (fs.aggr.op == TOKarrayliteral &&
!((*fs.parameters)[dim - 1].storageClass & STCref))
{
auto ale = cast(ArrayLiteralExp)fs.aggr;
size_t edim = ale.elements ? ale.elements.dim : 0;
fs.aggr.type = tab.nextOf().sarrayOf(edim);
auto telem = (*fs.parameters)[dim - 1].type;

// Bugzilla 12936: if telem has been specified explicitly,
// converting array literal elements to telem might make it @nogc.
fs.aggr = fs.aggr.implicitCastTo(sc, telem.sarrayOf(edim));
if (fs.aggr.op == TOKerror)
goto Lerror2;

// for (T[edim] tmp = a, ...)
tmp = new VarDeclaration(loc, fs.aggr.type, id, ie);
Expand Down
18 changes: 18 additions & 0 deletions test/runnable/nogc.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ void test12642() @nogc
data2 = foo12642();
}

/***********************/
// 12936

void test12936() @nogc
{
foreach (int[1] a; [[1]])
{
assert(a == [1]);
}
foreach (i, int[1] a; [[1], [2]])
{
if (i == 0) assert(a == [1]);
else if (i == 1) assert(a == [2]);
else assert(0);
}
}

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

int main()
{
test1();
test3032();
test12642();
test12936();

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

0 comments on commit 6a48d5b

Please sign in to comment.