Skip to content

-mm=own: copyable any, rest-parameter takes, @sharesElements - the DefaultLib own skips lifted - #753

Merged
ASDAlexander77 merged 4 commits into
mainfrom
own-any-copies
Oct 30, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
own-any-copies

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Lifts the DefaultLib tests that were skipped under -mm=own (all but weakref_basic, which stays gc-only). DefaultLib side: ASDAlexander77/TypeScriptCompilerDefaultLib branch own-any-elements.

Changes

  • An any is copied as a string is (spec 71). Where own needs a second owner of an any (pushed twice, kept in two maps), it copies the box. An any holding a string copies the string; one holding an object stops the program: "-mm=own cannot copy an 'any' that holds an object; give the object a Shared". Map<any, any> runs under own.
  • @copies lets a function copy arrays of values (only reduce / reduceRight use it), since array copies are visible and must not appear elsewhere.
  • <any>42 is not a literal: k.keep(42) freed the box twice (also on main).
  • Elements taken out of a rest parameter (spec 72). A function that shifts or pops objects out of ...args and keeps them keeps the parameter: callers move their elements in. Taking one out without keeping it is an error. This closes a double free on list.push(local) (on main). Numbers taken out are copies and need nothing.
  • @sharesElements (spec 73). A generic class's methods are all instantiated with it, so Array<Item> failed on slice, concat and the others though nothing called them. A method marked @sharesElements and instantiated for an object element gets a stop as its body; a call to it is an error: "'slice' would give an element of type 'Item' a second owner, which -mm=own cannot do; hold the elements as 'Shared'".
  • A generator over an immortal global is not reported as outliving it.
  • Spec §71-73 in the own design doc.

Tests

  • New: own_any_copies, own_array_copies, own_string_pushed_twice, own_any_object_copy_stops, own_rest_takes, own_err_rest_take_dropped, own_err_rest_take_used_after, own_shares_elements, own_err_shares_elements_called.
  • Release ctest 6466/6466 on Windows.
  • DefaultLib with this compiler: own 181/182 on Windows and Linux (release and debug, compile and JIT; only weakref_basic skipped); gc 182/182, rc and none 181/182 on Windows.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 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>
@ASDAlexander77
ASDAlexander77 merged commit d361cbd into main Oct 30, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-any-copies branch October 30, 2026 22:43
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