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

Bug6652 #377

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/statement.c
Expand Up @@ -1597,13 +1597,15 @@ Statement *ForeachStatement::semantic(Scope *sc)
Parameter *arg = arguments->tdata()[i];
Type *argtype = arg->type->semantic(loc, sc);
VarDeclaration *var;
int iskey = dim == 2 && i == 0;

var = new VarDeclaration(loc, argtype, arg->ident, NULL);
Identifier *id = !iskey || (arg->storageClass & STCref) ? arg->ident : Lexer::uniqueId("__key");
var = new VarDeclaration(loc, argtype, id, NULL);
var->storage_class |= STCforeach;
var->storage_class |= arg->storageClass & (STCin | STCout | STCref | STC_TYPECTOR);
if (var->storage_class & (STCref | STCout))
var->storage_class |= STCnodtor;
if (dim == 2 && i == 0)
if (iskey)
{ key = var;
//var->storage_class |= STCfinal;
}
Expand Down Expand Up @@ -1668,11 +1670,25 @@ Statement *ForeachStatement::semantic(Scope *sc)
// key += 1
increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1));

Statements *bodyinit = new Statements;
if (dim == 2 && !(arguments->tdata()[0]->storageClass & STCref))
{
// key = __key
bodyinit->reserve(3);
Parameter *arg = arguments->tdata()[0];
ExpInitializer *ie = new ExpInitializer(loc, new VarExp(loc, key));
VarDeclaration *argcopy = new VarDeclaration(loc, arg->type, arg->ident, ie);
bodyinit->push(new ExpStatement(loc, argcopy));
}
else
bodyinit->reserve(2);

// T value = tmp[key];
value->init = new ExpInitializer(loc, new IndexExp(loc, new VarExp(loc, tmp), new VarExp(loc, key)));
Statement *ds = new ExpStatement(loc, value);
bodyinit->push(new ExpStatement(loc, value));

body = new CompoundStatement(loc, ds, body);
bodyinit->push(body);
body = new CompoundStatement(loc, bodyinit);

s = new ForStatement(loc, forinit, cond, increment, body);
s = s->semantic(sc);
Expand Down Expand Up @@ -2270,12 +2286,13 @@ Statement *ForeachRangeStatement::semantic(Scope *sc)
* for (auto tmp = lwr, auto key = upr; key-- > tmp;)
*/

Identifier *idkey = arg->storageClass & STCref ? arg->ident : Lexer::uniqueId("__key");
ExpInitializer *ie = new ExpInitializer(loc, (op == TOKforeach) ? lwr : upr);
key = new VarDeclaration(loc, arg->type, arg->ident, ie);
key = new VarDeclaration(loc, arg->type, idkey, ie);

Identifier *id = Lexer::uniqueId("__limit");
Identifier *idtmp = Lexer::uniqueId("__limit");
ie = new ExpInitializer(loc, (op == TOKforeach) ? upr : lwr);
VarDeclaration *tmp = new VarDeclaration(loc, arg->type, id, ie);
VarDeclaration *tmp = new VarDeclaration(loc, arg->type, idtmp, ie);

Statements *cs = new Statements();
// Keep order of evaluation as lwr, then upr
Expand Down Expand Up @@ -2318,6 +2335,15 @@ Statement *ForeachRangeStatement::semantic(Scope *sc)
//increment = new AddAssignExp(loc, new VarExp(loc, key), new IntegerExp(1));
increment = new PreExp(TOKpreplusplus, loc, new VarExp(loc, key));

if (!(arg->storageClass & STCref))
{
ExpInitializer *ie = new ExpInitializer(loc, new VarExp(loc, key));
VarDeclaration *argcopy = new VarDeclaration(loc, arg->type, arg->ident, ie);
Statement *ds = new ExpStatement(loc, argcopy);

body = new CompoundStatement(loc, ds, body);
}

ForStatement *fs = new ForStatement(loc, forinit, cond, increment, body);
s = fs->semantic(sc);
return s;
Expand Down
103 changes: 103 additions & 0 deletions test/runnable/foreach5.d
Expand Up @@ -106,6 +106,108 @@ void test7004()
func7004(1, 3.14);
}

/***************************************/
// 6652

void test6652()
{
size_t sum;

foreach (i; 0 .. 10)
sum += i++; // 0123456789
assert(sum == 45);

sum = 0;
foreach (ref i; 0 .. 10)
sum += i++; // 02468
assert(sum == 20);

sum = 0;
foreach_reverse (i; 0 .. 10)
sum += i--; // 9876543210
assert(sum == 45);

sum = 0;
foreach_reverse (ref i; 0 .. 10)
sum += i--; // 97531
assert(sum == 25);

enum ary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
sum = 0;
foreach (i, v; ary)
{
assert(i == v);
sum += i++; // 0123456789
}
assert(sum == 45);

sum = 0;
foreach (ref i, v; ary)
{
assert(i == v);
sum += i++; // 02468
}
assert(sum == 20);

sum = 0;
foreach_reverse (i, v; ary)
{
assert(i == v);
sum += i--; // 9876543210
}
assert(sum == 45);

sum = 0;
foreach_reverse (ref i, v; ary)
{
assert(i == v);
sum += i--; // 97531
}
assert(sum == 25);

static struct Iter
{
~this()
{
++_dtorCount;
}

bool opCmp(ref const Iter rhs)
{
return _pos == rhs._pos;
}

void opUnary(string op)() if(op == "++" || op == "--")
{
mixin(op ~ q{_pos;});
}

size_t _pos;
static size_t _dtorCount;
}

Iter._dtorCount = sum = 0;
foreach (v; Iter(0) .. Iter(10))
sum += v._pos++; // 0123456789
assert(sum == 45 && Iter._dtorCount == 12);

Iter._dtorCount = sum = 0;
foreach (ref v; Iter(0) .. Iter(10))
sum += v._pos++; // 02468
assert(sum == 20 && Iter._dtorCount == 2);

// additional dtor calls due to unnecessary postdecrements
Iter._dtorCount = sum = 0;
foreach_reverse (v; Iter(0) .. Iter(10))
sum += v._pos--; // 9876543210
assert(sum == 45 && Iter._dtorCount >= 12);

Iter._dtorCount = sum = 0;
foreach_reverse (ref v; Iter(0) .. Iter(10))
sum += v._pos--; // 97531
assert(sum == 25 && Iter._dtorCount >= 2);
}

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

int main()
Expand All @@ -114,6 +216,7 @@ int main()
test3187();
test5605();
test7004();
test6652();

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