Currently, DMD clutters the stack when a non-constant literal is used but never modified. For huge literal arrays, this ends up with a stack overflow.
The solution here would be to allocate space on the read-only section and point to that when used.
---
import core.stdc.stdio;
struct S
{
int[10000000] _;
}
int main() @nogc
{
foreach(f; [S()])
printf("%d\n", f._[0]);
return 0;
}
Comment #1 by maxhaton — 2022-05-10T01:49:07Z
This has probably been filed before. Can't be bothered to go looking at the moment.
Comment #2 by contact — 2022-05-10T01:58:47Z
(In reply to mhh from comment #1)
> This has probably been filed before. Can't be bothered to go looking at the
> moment.
The only issue I found was https://issues.dlang.org/show_bug.cgi?id=14451 but it is not entirely correlated. static foreach can yield stack variables intentionally. For normal foreach the situation is somewhat different. I expect a static foreach to produce stack space, although could be enhanced.
Maybe it is not "critical", but above "normal" importance.
Somewhat related to https://issues.dlang.org/show_bug.cgi?id=13381 .
Comment #3 by robert.schadek — 2024-12-13T19:22:42Z