Skip to content

Commit

Permalink
fix Issue 14625 - opIndex() doesn't work on foreach container iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Jun 18, 2015
1 parent 317f812 commit f3eba71
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/opover.c
Expand Up @@ -1480,12 +1480,12 @@ bool inferAggregate(ForeachStatement *fes, Scope *sc, Dsymbol *&sapply)
{
sapply = search_function(ad, idapply);
if (sapply)
{ // opApply aggregate
{
// opApply aggregate
break;
}

Dsymbol *s = search_function(ad, Id::slice);
if (s)
if (fes->aggr->op != TOKtype)
{
Expression *rinit = new ArrayExp(fes->aggr->loc, fes->aggr);
rinit = rinit->trySemantic(sc);
Expand Down
31 changes: 31 additions & 0 deletions test/runnable/opover2.d
Expand Up @@ -1979,6 +1979,36 @@ void test14624()
}
}

/**************************************/
// 14625

void test14625()
{
struct R
{
@property bool empty() { return true; }
@property int front() { return 0; }
void popFront() {}
}

struct C1
{
R opIndex() { return R(); }
R opSlice() { assert(0); }
}
C1 c1;
foreach (e; c1) {} // OK <- asserts in opSlice()
foreach (e; c1[]) {} // OK, opIndex()

struct C2
{
R opIndex() { return R(); }
}
C2 c2;
foreach (e; c2) {} // OK <- rejected
foreach (e; c2[]) {} // OK, opIndex()
}

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

int main()
Expand Down Expand Up @@ -2025,6 +2055,7 @@ int main()
test20c();
test20d();
test14624();
test14625();

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

0 comments on commit f3eba71

Please sign in to comment.