Skip to content

-mm=own: a rest parameter declared kept (@keeps) is released once emptied - #754

Merged
ASDAlexander77 merged 6 commits into
mainfrom
own-declared-keeps-rest
Oct 30, 2026
Merged

ASDAlexander77 merged 6 commits into
mainfrom
own-declared-keeps-rest

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Under -mm=own, a function whose rest parameter is declared kept with @keeps("args") never released the emptied rest array. pinFacts (OwnershipSignaturePass.cpp) set OWN_PARAMS_RELEASED only for functions without declared caller facts, so the exit release was skipped and every call leaked the array. An inferred keep (spec 72, own_rest_takes.ts) was already fine.

Fix: a declared keep of the rest parameter now gets the same release as an inferred one. Other declared keeps are unchanged.

Stacked on #753 (own-any-copies, spec 72). Until that merges, this PR also shows its commits; the new change is the top commit only.

Tests

  • own/own_rest_keeps_declared.ts: a @keeps("args") method that shifts elements out, run under own (jit, compile, no-counting), rc, gc and none.
  • test-own-rest-keeps-declared-released (own-rest-keeps-declared.cmake): reads the --emit=mlir-affine IR and checks that List.push releases its rest array. It fails on the baseline binary and passes with the fix.
  • Release ctest: 6476/6476.

DefaultLib currently relies on inference for this case. With this fix, it could declare @keeps instead.

🤖 Generated with Claude Code

ASDAlexander77 and others added 6 commits October 30, 2026 21:27
…nerators over immortal globals

Under own a second owner own cannot prove becomes a copy for a string (spec 22). Now for an `any`
box too, so an untyped Map (Map<any, any>) and a @SharedElements container of `any` work:
- ts.StringCopy of an `any` lowers to a new box of the same bytes. A payload that owns nothing is
  copied with them; a string payload is copied in turn. An object payload has one owner, the box:
  the copy stops the program with a message, as a failed assert does.
- copyForRetain: of several uses (`f(s); f(s)`, `push(s, s)`) the retain copies for the first after
  it in its block, as rc retained each beside its use. own_err_string_pushed_twice pinned the old
  rule and said to become a positive when it widened: it is own_string_pushed_twice.
- A constant boxed into `any` (`k.keep(42)`) or printed into a string was taken for a literal that
  holds no block, so its release stayed after the move and the box was freed twice. On main too.
- An array whose elements own nothing is copied the same way, but only in a function that says
  `@copies`: a copy of an array can be told from it. The default library's reduce and reduceRight,
  whose accumulator starts as an element, say it.
- A generator kept in a global that borrows only immortal globals outlives nothing
  (`const it = map.values()` at the top of a script).

Tests: own_any_copies, own_array_copies, own_string_pushed_twice (own, no counting),
own_any_object_copy_stops. own_err_shared_elements_any is gone: `any` is an element now.
Release ctest 6457/6457.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…st.push(local)` closed

The array a caller gives a rest parameter borrows its elements when they are the caller's (spec 49),
but a `pop` or `shift` of it was a value of the function's own: `this.data.push(args.shift())`
compiled, and `list.push(item)` freed `item` twice, as did `const first = items.shift()` dropped.
- A function that takes an element out of its rest parameter and keeps it keeps the parameter: its
  callers move what they give into the array (a local is then moved), and the function releases the
  emptied array at its exits, as it releases a parameter kept on some paths.
- An element taken out of a rest parameter the function does not keep is not its own, and the take
  is an error: read it by its index instead.
This is how an Array<T> of objects pushes, unshifts and splices under own.

Tests: own_rest_takes (own, no counting), own_err_rest_take_dropped, own_err_rest_take_used_after.
Release ctest 6462/6462.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… error where it is called

A generic class's methods are all made with it, so Array<Item> failed under own on slice, concat,
fill, filter and the others that give an element a second place, though nothing called them. Such
a method or function says `@sharesElements`:
- MLIRGen marks what it makes for a type argument that owns a block of its own and is not a string,
  an `any` or a handle (OWN_CANNOT_SHARE_ATTR_NAME): a fact of the types, the same in every model.
- The own signature pass, first, makes its body a stop (an assert and a default return), keeping its
  vtable slot. What the removed body made for itself - a generator's `next`, named in its constant
  state record - goes with it once nothing else refers to it, and so do its own markers.
- Inference reports a call or a reference to it: "'slice' would give an element of type 'Item' a
  second owner, which -mm=own cannot do; hold the elements as 'Shared<Item>'".

Spec 71-73 written for this branch (any copies and @copies, rest-parameter takes, this).
Tests: own_shares_elements (own, rc, gc, none, no counting), own_err_shares_elements_called.
Release ctest 6466/6466.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…body keeps its own debug scope

- takenOutOfBorrowedRest held for any pop/shift of a rest parameter, so a function that summed
  `...args: number[]` with args.pop() was an error under own (DefaultLib array_every and
  array_constructors_overload). Only an element that owns a block is the caller's.
- stubCannotShare gave the new body the function's location; with --di LLVM said the !dbg
  attachment pointed at the wrong subprogram. It takes the old body's first location.

Tests: own_rest_takes sums numbers out of a rest parameter. Release ctest 6466/6466.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d once emptied

A function whose rest parameter is inferred kept (spec 72) releases the emptied array at its exits,
but one that says `@keeps("args")` never did: pinFacts set OWN_PARAMS_RELEASED only for functions
without declared caller facts, so every call leaked its rest array. A declared keep of the rest
parameter is now released the same way an inferred one is; other declared keeps are unchanged.

Tests: own_rest_keeps_declared (own, no counting, rc, gc, none) and
test-own-rest-keeps-declared-released, which reads the IR (fails before the fix).
Release ctest 6476/6476.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 787f39c into main Oct 30, 2026
0 of 2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-declared-keeps-rest branch October 30, 2026 23:03
ASDAlexander77 added a commit that referenced this pull request Oct 30, 2026
The merges of #754 and #756 each added their test to the own `foreach` lists without dropping the
line it replaced, so `tslang/test/tester/CMakeLists.txt` closed the list early and CMake failed to
configure ("Parse error. Expected "(", got identifier with text "own_callback_cast""). Both lists
now hold each name once: own_equal_strings_into_any, the spec 72/73 tests and own_rest_keeps_declared.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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