01 Sources: Increment on Yield
The foundation of the ownership protocol. Source iterators that yield borrowed copies of collection elements must increment the element’s RC so the yielded copy is independently owned. Source iterators that yield fresh values (Range yields ints, Option yields a copy) already produce owned elements.
01.1 Extend IterState::List with elem_inc_fn
File: compiler/ori_rt/src/iterator/state.rs
The List variant of IterState currently stores data, len, cap, elem_size (line ~53). It needs an elem_inc_fn: Option<extern "C" fn(*mut u8)> field.
-
Add
elem_inc_fn: Option<extern "C" fn(*mut u8)>toIterState::Listvariant -
Update the
Dropimpl forList— no change needed (it already usesori_buffer_rc_decwhich readselem_dec_fnfrom the V5 header) -
Verify
IterStatesize doesn’t blow the cache line budget (currently fits in ~128 bytes for largest variant) -
Subsection close-out (01.1) — MANDATORY before starting 01.2:
- All tasks above are
[x]and behavior verified - Update this subsection’s
statusin section frontmatter tocomplete - Run
/improve-toolingretrospectively on THIS subsection - Run
/sync-claudeon THIS subsection — check whether code changes invalidated any CLAUDE.md,.claude/rules/*.md, orcanon.mdclaims. If no API/command/phase changes, document briefly. Fix any drift NOW. - Repo hygiene check — run
diagnostics/repo-hygiene.sh --checkand clean any detected temp files.
- All tasks above are
01.2 Extend ori_iter_from_list Signature
File: compiler/ori_rt/src/iterator/sources.rs
ori_iter_from_list (line 28) currently takes (data: *mut u8, len: i64, cap: i64, elem_size: i64). Add elem_inc_fn.
- Change signature to
(data, len, cap, elem_size, elem_inc_fn: Option<extern "C" fn(*mut u8)>) - Store
elem_inc_fnin theIterState::Listvariant - Update all callers in
ori_llvm—emit_iter_from_listincompiler/ori_llvm/src/codegen/arc_emitter/builtins/iterator.rsmust pass the new parameter - Runtime tests in
compiler/ori_rt/src/iterator/tests.rs— updateori_iter_from_listcalls to passNone(existing tests use int elements = scalar = no inc needed)
C ABI contract: elem_inc_fn is Option<extern "C" fn(*mut u8)> — same type as elem_dec_fn. Null for scalar elements. For RC elements (str, lists, closures), it’s the same function generated by get_or_generate_elem_inc_fn() in element_fn_gen.rs.
- Subsection close-out (01.2) — MANDATORY before starting 01.3:
- All tasks above are
[x]and behavior verified - Update this subsection’s
statusin section frontmatter tocomplete - Run
/improve-toolingretrospectively on THIS subsection - Run
/sync-claudeon THIS subsection — check whether code changes invalidated any CLAUDE.md,.claude/rules/*.md, orcanon.mdclaims. If no API/command/phase changes, document briefly. Fix any drift NOW. - Repo hygiene check — run
diagnostics/repo-hygiene.sh --checkand clean any detected temp files.
- All tasks above are
01.3 Implement Inc in next_list
File: compiler/ori_rt/src/iterator/next.rs
next_list (lines 96-104) currently does a raw ptr::copy_nonoverlapping from the list buffer to out_ptr. After the copy, call elem_inc_fn on the destination pointer (the yielded copy).
- After
ptr::copy_nonoverlapping(data.add(offset as usize), out_ptr, es as usize)at line 101, add:
whereif let Some(inc) = self.elem_inc_fn() { inc(out_ptr); }elem_inc_fn()is a method on IterState that returns the stored function pointer for List variants (or None for others). - TDD: Write failing test FIRST — create an AOT test that joins mapped strings from a list, verifying zero leaks under
ORI_CHECK_LEAKS=1. This test currently leaks; after the inc fix + consumer dec fix (Section 03), it will pass. - Verify
next_listfor scalar elements (int, float, bool) —elem_inc_fnis null, so the null-check short-circuits. Zero overhead for scalars.
Important: This change alone will cause DOUBLE RC for elements — the list still owns them AND the yielded copy now owns them. This is correct! The consumer will dec after use (Section 03), and the list’s Drop will dec the originals. Net effect: balanced.
- Subsection close-out (01.3) — MANDATORY before starting 01.4:
- All tasks above are
[x]and behavior verified - Update this subsection’s
statusin section frontmatter tocomplete - Run
/improve-toolingretrospectively on THIS subsection - Run
/sync-claudeon THIS subsection — check whether code changes invalidated any CLAUDE.md,.claude/rules/*.md, orcanon.mdclaims. If no API/command/phase changes, document briefly. Fix any drift NOW. - Repo hygiene check — run
diagnostics/repo-hygiene.sh --checkand clean any detected temp files.
- All tasks above are
01.4 Verify Other Sources (Range, Str, Map, Option)
Files: compiler/ori_rt/src/iterator/sources.rs, next.rs
-
Range (
next_range, line 106-140): yieldsi64values. Scalar — no RC. No change needed. Verify. -
Str (
next_str): yields individual characters/codepoints. These are scalar values extracted from the string. No RC on individual chars. No change needed. Verify. -
Map (
next_map, state.rsMapvariant): yields(key, value)pairs from map entries. TheMapvariant already storeskey_dec_fnandval_dec_fn. Need to also storekey_inc_fnandval_inc_fnand call them innext_map. Updateori_iter_from_mapsignature. -
Option (
ori_iter_from_option, sources.rs line 165-195): yields a single element fromSome. The element is already copied into a 1-element buffer withelem_dec_fnstored in the header. Need to add inc on yield from that buffer (same pattern as list). -
Subsection close-out (01.4) — MANDATORY before starting Section 02:
- All tasks above are
[x]and behavior verified - Update this subsection’s
statusin section frontmatter tocomplete - Run
/improve-toolingretrospectively on THIS subsection - Run
/sync-claudeon THIS subsection — check whether code changes invalidated any CLAUDE.md,.claude/rules/*.md, orcanon.mdclaims. If no API/command/phase changes, document briefly. Fix any drift NOW. - Repo hygiene check — run
diagnostics/repo-hygiene.sh --checkand clean any detected temp files.
- All tasks above are
01.R Third Party Review Findings
- None.
01.N Completion Checklist
- All source
next_*functions yield owned elements -
timeout 150 ./test-all.shgreen (note: some tests may temporarily leak MORE because consumers don’t dec yet — this is expected until Section 03) -
timeout 150 ./clippy-all.shgreen -
/commit-push -
/tpr-reviewpassed -
/impl-hygiene-reviewpassed -
/improve-toolingsection-close sweep