This is a variation of bug 2437, but ICEs in a completely different place. --- struct Foo{ int z; this(int a){z=a;} } void bar(Foo a = Foo(1)){ } void foo() { bar(); } --- Internal error: ..\ztc\out.c 1199
Same root cause as bug 2437, and the same simple patch fixes it.
Not patched yet, see bug 2437 for details.
Note that struct defaults using either opAssign or static opCall do not seem to be affected by this bug.
*** Issue 3399 has been marked as a duplicate of this issue. ***
*** Issue 3648 has been marked as a duplicate of this issue. ***
PATCH: I think it's enough to change tocsym.c, VarDeclaration::toSymbol(), around line 201. If it's a CTFE variable, it's shouldn't be marked as an extern. t->Tcount++; - if (isDataseg()) + if (isDataseg() && !isCTFE()) { if (isThreadlocal()) { /* Thread local storage */ TYPE *ts = t; ts->Tcount++; // make sure a different t is allocated
The problem is a semantic one. Default arguments are evaluated in the context of the function declaration, not where it's used. So, the temporary generated by the constructor is created in global space! The patch tries to force it back into the function scope, but that doesn't work as you can see if you change foo() to: void foo() { bar(); bar(); } as it fails trying to allocate the same symbol twice. Not sure what the correct solution is at the moment.
The problem is the code here in expression.c funcParameters(): arg = p->defaultArg; arg = arg->copy(); <-- Danger, Will Robinson! arg = arg->resolveLoc(loc, sc); arguments->push(arg); The arg->copy() is the problem, as it will copy any DeclarationExp's resulting in multiple declarations with the same name. A correct fix will be to do what DeclarationExp::doInline() does, which is for any non-static declarations, create another declaration. A new expression tree walker has to be built to accomplish this. Perhaps a good approach is to create a generic walker that accepts a lambda function to operate on each node.
changeset 452
Fixed DMD2.044