Skip to content

Commit

Permalink
Fix Issue 9212 - Associative array foreach iteration with immutable key
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Nov 23, 2013
1 parent 8f1d03a commit 05d96ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/statement.c
Expand Up @@ -2106,13 +2106,14 @@ Statement *ForeachStatement::semantic(Scope *sc)
{ error("foreach: index cannot be ref");
goto Lerror2;
}
if (!arg->type->equals(taa->index))
if (!taa->index->implicitConvTo(arg->type))
{ error("foreach: index must be type %s, not %s", taa->index->toChars(), arg->type->toChars());
goto Lerror2;
}
arg = (*arguments)[1];
}
if (!arg->type->equals(taa->nextOf()))
if ((!arg->type->equals(taa->nextOf()) && (arg->storageClass & STCref)) ||
!taa->nextOf()->implicitConvTo(arg->type))
{ error("foreach: value must be type %s, not %s", taa->nextOf()->toChars(), arg->type->toChars());
goto Lerror2;
}
Expand Down
9 changes: 9 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -1492,6 +1492,15 @@ void test74()

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

void test9212()
{
int[int] aa;
foreach (const key, const val; aa) {}
foreach (size_t key, size_t val; aa) {}
}

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

class A75
{
pure static void raise(string s)
Expand Down

0 comments on commit 05d96ad

Please sign in to comment.