-
-
Notifications
You must be signed in to change notification settings - Fork 428
Fix wrong code generation for this-> in lambda type parameters
#4955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ## Fix wrong code generation for `this->` in lambda type parameters | ||
|
|
||
| Using `this->` in a lambda type parameter (e.g., `{(this->A!): Bool}`) and then forwarding that lambda to another function produced wrong code at runtime. Depending on the program, you'd get incorrect results, wrong vtable dispatch, or a segfault. Calling the lambda directly in the same method worked fine — the bug only appeared when the lambda was passed along. | ||
|
|
||
| The root cause: lambda types are desugared into anonymous interfaces, and `this->` was copied verbatim into the interface. Once inside the interface, `this` referred to the interface's own receiver instead of the enclosing class's receiver, producing the wrong type for structural subtype checks. | ||
|
|
||
| `this->` is now resolved to the enclosing method's receiver capability during desugaring, so `{(this->A!)}` in a `box` method correctly becomes `{(box->A!)}`. | ||
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
1 change: 1 addition & 0 deletions
1
test/full-program-tests/issue-4168-lambda-type-this-viewpoint/expected-exit-code.txt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 1 |
37 changes: 37 additions & 0 deletions
37
test/full-program-tests/issue-4168-lambda-type-this-viewpoint/main.pony
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """ | ||
| Regression test for issue #4168. When this-> appears in a lambda type parameter | ||
| (e.g. {(this->A!)}), it must resolve to the enclosing method's receiver cap, | ||
| not the desugared interface's receiver. Without the fix, forwarding the lambda | ||
| to another function caused wrong vtable dispatch and segfaults. | ||
| """ | ||
| use @pony_exitcode[None](code: I32) | ||
|
|
||
| class Container[A: Any #read] | ||
| let _data: A | ||
|
|
||
| new create(a: A) => | ||
| _data = a | ||
|
|
||
| fun apply(): this->A => | ||
| _data | ||
|
|
||
| // this-> in the lambda type is the key construct being tested. | ||
| // It should resolve to box-> since fun defaults to box receiver. | ||
| fun update(f: {(this->A!)}) => | ||
| Applier[A, this->Container[A]](this, f) | ||
|
|
||
| primitive Applier[A: Any #read, B: Container[A] #read] | ||
| fun apply(c: B, f: {(B->A!)}) => | ||
| f(c.apply()) | ||
|
|
||
| class Counter | ||
| var value: I32 = 0 | ||
|
|
||
| actor Main | ||
| new create(env: Env) => | ||
| let c = Container[Counter](Counter) | ||
| c.update({(x: Counter box) => | ||
| if x.value == 0 then | ||
| @pony_exitcode(1) | ||
| end | ||
| }) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong.
Discussed in sync.
Consider a type
Foo, which has afun box, in which a lambda is born withthis->A!as the parameter typeWe need the lambda to get sugared to a generic type, where the type parameter has a constraint of
Foo #read. Then The generic anonymous lambda type gets instantiated at the call site with a type parameter ofthis->Foo'ref, which is effectively the way to "pass in" the correct constrained generic cap of thethis(of thefun box).As I said in the sync call,
fun boxis actually in realityfun #readbut we call itfun boxfor historical/aesthetic reasons.