[Issue 17819] static foreach segfaults on __traits(allMembers)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 2 17:57:06 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=17819

timon.gehr at gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr at gmx.ch

--- Comment #6 from timon.gehr at gmx.ch ---
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.

--


More information about the Digitalmars-d-bugs mailing list