Fix __traits(getMember, this, ...) not capturing enclosing this in nested functions - #23437
Open
niy-ati wants to merge 1 commit into
Open
Fix __traits(getMember, this, ...) not capturing enclosing this in nested functions#23437niy-ati wants to merge 1 commit into
niy-ati wants to merge 1 commit into
Conversation
…sted functions 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 and the compiler omits vthis from the closure frame. After expressionSemantic on the resolved member, when the lookup object is this, call checkNestedReference on te.var so closure building matches a direct field access. Fixes dlang#23436.
dkorpel
requested changes
Jul 20, 2026
Comment on lines
+1170
to
+1185
| // 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(); | ||
| } | ||
| } | ||
| } |
Contributor
There was a problem hiding this comment.
This kind of "if (code == issue) fix();" patch is not robust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
__traits(getMember, this, "field")inside a nested function does not register a nested reference to the enclosing memberthis. The compiler then omitsvthisfrom the closure frame, causing a segfault when the delegate runs.getMemberwithwantsymreturns the memberVarDeclarationwithout keeping aThisExpuse. AfterexpressionSemanticon the resolved member, when the lookup object isthis, this patch callscheckNestedReferenceonte.varso closure building matches a direct field access.Test plan
Added
compiler/test/runnable/test_getMember_nested_this.d: nested function returned as a delegate that reads a field viagetMemberonthis.Fixes #23436.
Also fixes ldc-developers/ldc#5203.