Skip to content

Fix wrong code generation for this-> in lambda type parameters#4955

Open
SeanTAllen wants to merge 1 commit intomainfrom
fix-lambda-type-this-viewpoint
Open

Fix wrong code generation for this-> in lambda type parameters#4955
SeanTAllen wants to merge 1 commit intomainfrom
fix-lambda-type-this-viewpoint

Conversation

@SeanTAllen
Copy link
Member

When a lambda type contains this-> (e.g., {(this->A!): Bool}), the sugar pass desugars it into an anonymous interface. The desugaring preserved this-> verbatim, but inside the generated interface, this refers to the interface's own receiver rather than the enclosing class's receiver. This caused incorrect structural subtype checks, leading to wrong vtable dispatch, incorrect results, or segfaults when the lambda was forwarded to another function rather than called directly.

The fix resolves this-> to the enclosing method's receiver cap during lambda type desugaring, so {(this->A!)} in a box method correctly becomes {(box->A!)}.

Closes #4168

Lambda types like `{(this->A!): Bool}` are desugared into anonymous
interfaces during the sugar pass. The desugaring preserved `this->`
verbatim, but inside the generated interface, `this` refers to the
interface's own receiver rather than the enclosing class's receiver.
This semantic mismatch caused incorrect structural subtype checks,
leading to wrong vtable dispatch, incorrect results, or segfaults
when the lambda was forwarded to another function.

The fix resolves `this->` to the enclosing method's receiver cap
during lambda type desugaring. Since the compiler already enforces
that `this->` can only appear in `box` methods, `{(this->A!)}` now
correctly desugars to `{(box->A!)}`.

Closes #4168
@SeanTAllen SeanTAllen added the changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge label Mar 7, 2026
@ponylang-main ponylang-main added the discuss during sync Should be discussed during an upcoming sync label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog - fixed Automatically add "Fixed" CHANGELOG entry on merge discuss during sync Should be discussed during an upcoming sync

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong code generation/memory errors in generic lambda parameters

2 participants