Iterator Element Ownership Protocol
6 sections
Overview
Establish a uniform element ownership contract for the C iterator runtime protocol: every element yielded by IterState::next() is owned (+1 RC) by the consumer. Consumers dec after use; adapters that discard elements dec before discarding. This eliminates BUG-05-003 (leaked owned elements from map/flat_map adapters) and the broader class of borrow-vs-own ambiguity bugs across all 11 consumer functions, 11 adapter variants, and the AIMS analysis pipeline.
Planned
6 sections
Sources: Increment on Yield
Every source iterator's next_* function yields owned (+1 RC) elements
Adapters: Decrement on Discard
Adapters that discard elements (filter, skip) call elem_dec_fn; adapters that pass through or produce inherit correct ownership
Consumers: Decrement After Use
Every consumer function receives elem_dec_fn and calls it after consuming each element
Codegen: Parameter Plumbing
LLVM codegen passes elem_inc_fn and elem_dec_fn to all runtime functions that need them
AIMS: Owned Element Contract
AIMS analysis treats iterator-yielded elements as owned, removing borrowed suppression rules
Verification Matrix
Full consumer x adapter x type test matrix with leak checking