Skip to content

-mm=own: a generator that says @keeps owns the arguments its state holds - #699

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-generator-keeps
Oct 27, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-generator-keeps

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

Under -mm=own a generator's state captured its parameters as borrows (spec §19). The result of a lazy helper like iter.map(f) was therefore bounded by its arguments: it could not be stored, returned or handed to another generator. @keeps on a generator now names the parameters its state takes over:

  • Callers move the named arguments in, and the state's release frees them.
  • A @keeps generator is not bounded by its arguments.
  • A captured parameter left out of @keeps is an error ("names 'iter', but its body keeps 'iter', 'f'").
  • Using an argument after the call is the usual use-after-move error.
  • A generator without @keeps still borrows, as before.

Implementation:

  • paramsCapturedIntoState (OwnershipSignatureGenerators) finds the heap-owning parameter cells a generator's state captures. localOwnedParams counts them as kept when the maker says @keeps.
  • findBorrowingStates marks such a state owns. Its captured box is not a borrowing field, so the release routine frees it, and the result is not marked bounded.
  • decideEscaping no longer reports a kept parameter's cell as "its value is the caller's".
  • isFresh accepts an extension call to a defined function, and holdsNoBlock accepts a function reference cast.

Spec §38 and docs/own-memory-model.md are updated.

DefaultLib uses this for take/filter/map/drop in ASDAlexander77/TypeScriptCompilerDefaultLib (PR to follow).

Tests

  • New tests:
    • own_generator_keeps: a class argument, an iterator argument, an iterator plus a callback, a @keeps generator nested in another, a generator returned from a function or stored in a global, and one given up after one step. It churns between steps and runs under own, rc, gc and none.
    • Three error tests: @keeps missing a parameter, an argument used after the move, and a generator without @keeps that is stored.
  • Full ctest -C Release: 5872 passed, 0 failed.
  • DefaultLib suites with the companion change:
    • rc, gc and none pass every test.
    • own goes from 137 to 139/170 (iterator_map, iterator_filter), with no new failures.

🤖 Generated with Claude Code

A generator's state captured its parameters as borrows (spec 19), so a lazy helper such as
`iter.map(f)` could not be stored or returned past the expression that built it. `@keeps` on a
generator now names the parameters its state takes over: callers move them in, the state's release
frees them, and the generator is not bounded by its arguments. Spec section 38.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 9113970 into main Oct 27, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-generator-keeps branch October 27, 2026 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant