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
5 changes: 5 additions & 0 deletions .release-notes/5077.md
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.
Comment on lines +1 to +5
Copy link
Copy Markdown
Member

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.

3 changes: 2 additions & 1 deletion src/libponyc/pass/sugar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 sugar_match_capture around line 690 in this same file:

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, with (_, _) = ... only reports the first error. The user has to fix, recompile, discover the next one. The accumulation pattern reports all of them at once. Same fix, better UX.

}

return true;
Expand Down
37 changes: 37 additions & 0 deletions test/libponyc/with.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you switch to the accumulation pattern, add a test for (_, _) that expects two errors. That's the case where early return hides the second diagnostic.


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);
}