Fix nested references to 'ref' foreach variables.

These "walk around" the array being iterated over, so they're a bit trickier
than other variables to get right.
This commit is contained in:
Frits van Bommel
2009-04-01 00:01:44 +02:00
parent aa8aad611c
commit 3f49ddb6d5
6 changed files with 68 additions and 17 deletions

View File

@@ -1 +1,8 @@
void foo() { void delegate()[] bar; try {} finally { foreach (dg; bar) dg(); } }
void foo() {
void delegate()[] bar;
try {
} finally {
foreach (dg; bar)
dg();
}
}

19
tests/mini/foreach10.d Normal file
View File

@@ -0,0 +1,19 @@
module foreach10;
extern(C) int printf(char*, ...);
void main() {
char* last = null;
printf("The addresses should increment:\n");
foreach (ref c; "bar") {
auto a = {
printf("%x '%c'\n", c, c);
return &c;
};
auto nw = a();
printf("ptr = %p\n", nw);
if (last != null)
assert(nw == last+1);
last = nw;
}
}

18
tests/mini/foreach11.d Normal file
View File

@@ -0,0 +1,18 @@
module foreach11;
extern(C) int printf(char*, ...);
void main() {
char* last = null;
printf("The addresses should remain constant:\n");
foreach (c; "bar") {
auto a = {
printf("%x '%c'\n", c, c);
printf("ptr = %p\n", &c);
if (last)
assert(last == &c);
};
a();
last = &c;
}
}