Repository navigation
-mm=own: sort and reverse over class instances (swap op, forwarded callbacks) - #703
Merged
Merged
Conversation
…nto a place A function that returns `this` has a result that borrows `this`, and spec 22.4 kept every string read out of it from being copied - so `this[t] = this[s]` (copyWithin) and a swap's `let v = this[i]` (reverse) were "borrows an element". The fact is about what is returned: `copyForRetain` now copies such a read stored into an element or a field, and starts a `let` from a copy when the local cannot be returned (`mayBeReturned`). A read returned, directly or through a local, stays the borrow. Spec section 41. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A swap through a local, `let v = a[i]; a[i] = a[j]; a[j] = v`, gives an object element a second owner, so `sort` and `reverse` over class instances did not compile under own. An expression statement `[p, q] = [q, p]` whose two sides name the same two elements or fields of one type is now a `ts.Swap` of the two places: every value keeps one place, so it is emitted with no retain, release or drop in any model, and its lowering loads both before it stores either. The inference treats a swap as a drop of a borrow read out of either place (it is the other place's now, perhaps under another root), and the signature pass as an overwrite of both places for the callers. Spec section 42; docs limits bullet updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…backs `reverse` over class instances works with the exchange (spec 42); `sort` does not yet, because it gives its optional comparison on to a helper, and a callback given on through a parameter has no callback facts. The limits bullet and spec section 42 say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A callback parameter (a function value, or an optional one) that is only called, tested for being given, unwrapped or passed to one call is in a family whose members are the functions passed for it - including those passed for the caller's own parameter it is given from. A family given on is closed only while the families it is given from and to are. A call of an optional callback once tested resolves through its family too. DefaultLib `sort` over class instances with a comparison compiles under own (spec 43). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # docs/own-memory-model.md # tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md # tslang/test/tester/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sortandreverseover arrays of class instances now compile and run under-mm=own(spec sections 42 and 43).Swaps that move nothing (spec 42)
A swap through a local,
let t = a[i]; a[i] = a[j]; a[j] = t, gives the object inta second owner. Under own that is an error.[p, q] = [q, p]that names the same two elements or fields of one type is now a new op,ts.Swap.ts.Swapemits no retain, release or drop in any memory model. Its LLVM lowering loads both values before storing either.A callback passed on through a parameter keeps its facts (spec 43)
DefaultLib
sortpasses its optional comparator on to a helper. A callback passed on through a parameter used to have no facts under own, socompare(a[i], a[j])was a use of the two elements after a call that may release them.Tests
own_swap_elementsandown_callback_forwardedrun under own, rc, gc and none.own_err_swap_borrow_used_after,own_err_swap_call_borrow_used_after,own_err_callback_forwarded_dropsandown_err_callback_forwarded_keeps.ctest -C Releasepasses locally.scripts/measure_memory_model.ps1): own and rc peak at 4.7 MB at 1k, 100k and 400k iterations.The DefaultLib PR that uses this goes with it.
🤖 Generated with Claude Code