Given: // staticforeach.d import std.traits; static foreach (m; __traits(allMembers, staticforeach)) { pragma(msg, m.stringof); } Try to compile: $ dmd staticforeach.d Segmentation fault (core dumped) $ dmd --version DMD64 D Compiler v2.076.0-dirty Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
The bug is still present in 2.078.
Still present in v2.078.3 - it seems that no one is looking into this.
There's a stack overflow in Dsymbol's _foreach, every time it recurses into a AttribDeclaration (such as the `static foreach') the stack gets deeper until it explodes.
Not really a fix, but a start https://github.com/dlang/dmd/pull/8125
https://issues.dlang.org/show_bug.cgi?id=18718 is very related.
This is not really a `static foreach` issue, but rather a general issue with how DMD handles (or rather, does not handle) circular dependencies. The following code runs into the same problem: --- module c; static if(__traits(allMembers,__traits(parent,{}))[0]=="c"){} --- Here, DMD explicitly catches the infinite recursion and prints: c.d(3): Error: error evaluating `static if` expression (The code that does this has a bug in it, therefore the error message is not useful: if (exp.op == TOK.error || nest > 100) { error(loc, (nest > 1000) ? "unresolvable circular `static if` expression" : "error evaluating `static if` expression"); return errorReturn(); } ) It is trivial to apply a similar "fix" for `static foreach`, but I doubt this is what Jean-Louis had in mind. Interestingly enough, this "works" with string mixins: module c; mixin("enum "~__traits(allMembers,c)[1]~"2 = 2;"); int x; pragma(msg, x2); pragma(msg, __traits(allMembers,c)[1]); (Clearly, the code actually has no reasonable interpretation, but DMD still successfully compiles it.) This is because `CompileDeclaration.include` just returns null. I can "fix" the `static foreach` case by returning "null" in case a call to "include" is already on the stack. In fact, I could do the same to the `static if` case to make that "work" too.
*** Issue 18718 has been marked as a duplicate of this issue. ***
https://github.com/dlang/dmd/pull/8213
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/ebbd8e92b61bcf05df443b62deeff0786581067c fix Issue 17819 - static foreach segfaults on __traits(allMembers) https://github.com/dlang/dmd/commit/b625e01f4464768d0e7b1b8119d8f6ca3543efa1 Merge pull request #8213 from tgehr/fix17819 fix Issue 17819 - static foreach segfaults on __traits(allMembers) merged-on-behalf-of: Razvan Nitu <RazvanN7@users.noreply.github.com>