Skip to content
Open
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
16 changes: 16 additions & 0 deletions compiler/src/dmd/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,22 @@ Expression semanticTraits(TraitsExp e, Scope* sc)
return ErrorExp.get();
}
}
// getMember with wantsym returns the member VarDeclaration
// without a ThisExp use, so nested functions that only access
// a field through getMember(this, ...) never register a nested
// reference to the enclosing `this`. Force that here.
if (ex && !ex.isErrorExp())
{
if (auto e0 = isExpression(o))
{
e0 = e0.expressionSemantic(sc);
if (auto te = e0.isThisExp())
{
if (te.var && te.var.checkNestedReference(sc, e.loc))
return ErrorExp.get();
}
}
}
Comment on lines +1170 to +1185

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This kind of "if (code == issue) fix();" patch is not robust

return ex;
}
else if (e.ident == Id.getVirtualFunctions ||
Expand Down
25 changes: 25 additions & 0 deletions compiler/test/runnable/test_getMember_nested_this.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// https://github.com/dlang/dmd/issues/23436
// __traits(getMember, this, "field") in a nested function must register
// a nested reference to the enclosing `this` so vthis appears in the
// closure frame.

struct Struct
{
int field = 42;

auto nestedThunk()
{
int inner()
{
return __traits(getMember, this, "field");
}
return &inner;
}
}

void main()
{
auto s = Struct();
auto dg = s.nestedThunk();
assert(dg() == 42);
}
Loading