fix Issue 22854 - static foreach byCodepoint segfault (2.099-rc.1)

This commit is contained in:
Iain Buclaw 2022-12-20 18:37:23 +01:00
parent a6e34f8ae8
commit 77935bf494
3 changed files with 49 additions and 2 deletions

View file

@ -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);
}
}

View file

@ -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() { }
}

View file

@ -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--; }
}