-
-
Notifications
You must be signed in to change notification settings - Fork 429
Fix with tuple only processing first binding in build_with_dispose #5077
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ## Fix `with` tuple: process every binding in dispose generation | ||
|
|
||
| The compiler walked only the first element of a tuple pattern in `with` when building dispose calls and checking for invalid `_` bindings. Later elements were skipped, so some programs compiled incorrectly (missing dispose for later bindings, or no error for `_` in a later position). | ||
|
|
||
| Tuple patterns now walk every element. Programs using multi-binding `with` get correct cleanup and diagnostics. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -592,7 +592,8 @@ static bool build_with_dispose(pass_opt_t* opt, ast_t* dispose_clause, ast_t* id | |
| { | ||
| pony_assert(ast_id(p) == TK_SEQ); | ||
| ast_t* let = ast_child(p); | ||
| return build_with_dispose(opt, dispose_clause, let); | ||
| if(!build_with_dispose(opt, dispose_clause, let)) | ||
| return false; | ||
cuiweixie marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I didn't catch this in my first pass. This should use the error accumulation pattern instead of early return. There's a precedent in bool r = true;
for(ast_t* p = ast_child(idseq); p != NULL; p = ast_sibling(p))
{
pony_assert(ast_id(p) == TK_SEQ);
ast_t* let = ast_child(p);
if(!build_with_dispose(opt, dispose_clause, let))
r = false;
}
return r;With early return, |
||
| } | ||
|
|
||
| return true; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,40 @@ TEST_F(WithTest, NoEarlyReturnFromWith) | |
| TEST_ERRORS_1(src, | ||
| "use return only to exit early from a with block, not at the end"); | ||
| } | ||
|
|
||
| TEST_F(WithTest, DontcareInTupleSecondBindingIsRejected) | ||
| { | ||
| // Regression: without the tuple-loop fix in build_with_dispose (sugar.c), | ||
| // this compiles with 0 errors (wrong). With the fix, exactly one error. | ||
| // build_with_dispose must recurse through every tuple element; a prior bug | ||
| // returned after the first binding only, so `_` in a later position was not | ||
| // diagnosed. | ||
| const char* src = | ||
| "class D\n" | ||
| " new create() => None\n" | ||
| " fun dispose() => None\n" | ||
| "actor Main\n" | ||
| " new create(env: Env) =>\n" | ||
| " with (a, _) = (D.create(), D.create()) do\n" | ||
| " None\n" | ||
| " end"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you switch to the accumulation pattern, add a test for |
||
|
|
||
| TEST_ERRORS_1(src, "_ isn't allowed for a variable in a with block"); | ||
| } | ||
|
|
||
| TEST_F(WithTest, TwoElementTupleWithCompiles) | ||
| { | ||
| // Happy path: both bindings get dispose calls; tuple loop must process every | ||
| // element (regression for early-return bug that only handled the first). | ||
| const char* src = | ||
| "class D\n" | ||
| " new create() => None\n" | ||
| " fun dispose() => None\n" | ||
| "actor Main\n" | ||
| " new create(env: Env) =>\n" | ||
| " with (a, b) = (D.create(), D.create()) do\n" | ||
| " None\n" | ||
| " end"; | ||
|
|
||
| TEST_COMPILE(src); | ||
| } | ||
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.
can you update this to show an example of pony code that failed due to the bug? we like to show an example of what previously didn't work but does now in our release notes. thanks.