diff --git a/compiler/src/dmd/cond.d b/compiler/src/dmd/cond.d index 65085f544f..b099852d17 100644 --- a/compiler/src/dmd/cond.d +++ b/compiler/src/dmd/cond.d @@ -218,12 +218,12 @@ extern (C++) final class StaticForeach : RootObject { if (aggrfe) { - return new ForeachStatement(loc, aggrfe.op, parameters, aggrfe.aggr.syntaxCopy(), s, loc); + return new ForeachStatement(loc, aggrfe.op, parameters, aggrfe.aggr, s, loc); } else { assert(rangefe && parameters.dim == 1); - return new ForeachRangeStatement(loc, rangefe.op, (*parameters)[0], rangefe.lwr.syntaxCopy(), rangefe.upr.syntaxCopy(), s, loc); + return new ForeachRangeStatement(loc, rangefe.op, (*parameters)[0], rangefe.lwr, rangefe.upr, s, loc); } } diff --git a/compiler/test/compilable/issue22854.d b/compiler/test/compilable/issue22854.d new file mode 100644 index 0000000000..4d5a518853 --- /dev/null +++ b/compiler/test/compilable/issue22854.d @@ -0,0 +1,20 @@ +// https://issues.dlang.org/show_bug.cgi?id=22854 +void test22854() +{ + static foreach (ch; SomeContainer().range) { } +} + +struct SomeContainer +{ + SomeRange range() { return SomeRange(); } + TypeWithDestructor data; +} + +struct TypeWithDestructor { ~this() { } } + +struct SomeRange +{ + int front() { return 0; } + bool empty() { return true; } + void popFront() { } +} diff --git a/compiler/test/runnable/issue22854.d b/compiler/test/runnable/issue22854.d new file mode 100644 index 0000000000..c4cae17f95 --- /dev/null +++ b/compiler/test/runnable/issue22854.d @@ -0,0 +1,27 @@ +// https://issues.dlang.org/show_bug.cgi?id=22854 +void main() +{ + uint loops = 0; + static foreach (i; 0 .. 50) + { + static foreach (ch; SomeContainer().range) + loops++; + } + assert(loops == 50 * 50); +} + +struct SomeContainer +{ + SomeRange range() { return SomeRange(); } + TypeWithDestructor data; +} + +struct TypeWithDestructor { ~this() { } } + +struct SomeRange +{ + int count = 50; + int front() { return count; } + bool empty() { return count <= 0; } + void popFront() { count--; } +}