Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/dmd/delegatize.d
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ private void lambdaSetParent(Expression e, FuncDeclaration fd)
alias visit = typeof(super).visit;
FuncDeclaration fd;

private void setParent(Dsymbol s)
{
VarDeclaration vd = s.isVarDeclaration();
FuncDeclaration pfd = s.parent ? s.parent.isFuncDeclaration() : null;
s.parent = fd;
if (!vd || !pfd)
return;
// move to fd's closure when applicable
foreach (i; 0 .. pfd.closureVars.dim)
Copy link
Member

@PetarKirov PetarKirov Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, one should be careful when using foreach (; 0 .. max) and changing the max inside the loop, as the emitted code caches it:
https://run.dlang.io/gist/run-dlang/b0b40d58474fb7c35b22803b2432c02f?compiler=dmd

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I notice that, but the code here breaks immediately after remove() is called.

{
if (vd == pfd.closureVars[i])
{
pfd.closureVars.remove(i);
fd.closureVars.push(vd);
break;
}
}
}

public:
extern (D) this(FuncDeclaration fd)
{
Expand All @@ -98,7 +117,7 @@ private void lambdaSetParent(Expression e, FuncDeclaration fd)

override void visit(DeclarationExp e)
{
e.declaration.parent = fd;
setParent(e.declaration);
e.declaration.accept(this);
}

Expand All @@ -107,7 +126,7 @@ private void lambdaSetParent(Expression e, FuncDeclaration fd)
if (e.lengthVar)
{
//printf("lengthVar\n");
e.lengthVar.parent = fd;
setParent(e.lengthVar);
e.lengthVar.accept(this);
}
}
Expand All @@ -117,7 +136,7 @@ private void lambdaSetParent(Expression e, FuncDeclaration fd)
if (e.lengthVar)
{
//printf("lengthVar\n");
e.lengthVar.parent = fd;
setParent(e.lengthVar);
e.lengthVar.accept(this);
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/compilable/test20063.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

struct S
{
void f(alias fun)() {}
}

auto handleLazily(T)(lazy T expr) {}

void main()
{
class C {}

S().f!(() => new C()).handleLazily;
}