Iterator-Collection RC Ownership Contract
6 sections
Overview
Establish a correct, complete, and tested ownership contract between iterators and their source collections in the Ori compiler's AOT pipeline. Currently, the contract has two critical bugs: (1) iterator elem_dec_fn is hardcoded NULL, so element cleanup fails when ori_iter_drop does the final buffer RC dec; (2) the for-yield lowering doesn't properly scope the source collection's lifetime, causing the AIMS pipeline to emit a spurious extra RcDec (double-free). This plan fixes both bugs and audits the full for-do/for-yield parity to prevent similar issues.
Completed
5 sections
Root Cause Analysis & Design
Document the full causal chain from codegen through runtime that produces leaked elements and double-frees in iterator-based loops, establishing the shared understanding needed for Sections 02-06
Fix For-Yield RC Scoping
Eliminate the spurious extra RcDec on the source collection in for-yield by properly scoping the collection variable so it dies at the Jump to header, matching the for-do pattern. Also fix the clear_mutable_names() workaround that breaks outer mutable variable assignment in for-yield AOT.
For-Do / For-Yield Parity Audit
Systematic comparison of for-do and for-yield ARC IR across all 6 implemented element types (5 list + 1 map; Set<str> deferred until type exists), verifying identical RC semantics
Comprehensive Test Matrix
Combinatorial test coverage: 6 implemented element types x 8 iteration patterns x 2 loop variants (93 tests: 81 active + 12 ignored catch bug; Set<str> deferred until type exists)
Verification & Merge Gate
Full verification pass confirming all fixes are correct, no regressions exist, and the branch is ready to merge