Skip to content

-mm=own: Map and Set of strings and numbers; generator methods over this - #693

Merged
ASDAlexander77 merged 2 commits into
mainfrom
own-generator-this-borrows
Oct 26, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
own-generator-this-borrows

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Makes the default library's Map and Set (lib.generics.ts, compiled into each program) compile under -mm=own for number, boolean and string keys and values. Design notes: tslang/docs/superpowers/specs/2026-10-26-own-generic-containers-design.md. The library side is ASDAlexander77/TypeScriptCompilerDefaultLib PR own-generic-iterators, which needs this one.

Generator methods over this

  • A generator method whose state borrows this (@borrows("this")) no longer keeps it. Before, w.iter() moved w into the generator, so w could not be used after the loop.
  • Works through the vtable of a class that is not @final, and through forwarders (entries() { return this.iter(); }); the bound check sees the object through ts.GetThis, so reassigning the object while its iterator is read is an error (it compiled and read freed memory).
  • A state field read only into a yielded tuple (yield [k, v], also cast to a named tuple) moves into the yield.

Strings as values (spec 22), more places

  • A tuple made of borrowed strings starts from copies.
  • A string read out of a place and given to a callback that may free it, or to a call that keeps it, gets a copy (released after the call when the callee does not keep it). A direct call that may free it is still an error, so @nodrops keeps its meaning.

Other analysis changes

  • A parameter kept on some paths only is released on the others (takenParams = parameters a move reaches every return from). Behavior change: own_err_param_kept_some_paths and own_err_string_call_keeps_twice now compile and are retired; the new positive tests cover them.
  • findBetween does not search past the read's own block: a continue path reads again.
  • A call's drops for length =, pop, shift, splice are the elements of arrays of that element type, not anything.
  • Callback families for methods that nothing overrides, called through the vtable; a closed family with no members calls nothing.

Not covered (in the spec): any or object elements (constructor from an array, object moved into an entry tuple; waits on a ruling about containers of owned values); untyped new Set([1, 2]) also instantiates Set<any> (MLIRGen, every model); JIT under own loses library facts for some calls (looks pre-existing, e.g. date_getTime).

Tests

  • New: own_generator_method_this, own_string_copy_call, own_param_kept_some_paths, own_method_callbacks, own_loop_read_again, own_length_drops_elements, own_tuple_param_kept (each also checked under gc, rc and none), own_err_generator_method_reassigned, own_err_callback_method_overridden.
  • Windows release ctest: 5825/5825.
  • DefaultLib suite (AOT release, with the DefaultLib branch): own 80 → 87 passing, no regressions; gc 167/167; rc 166/167 (weakref_basic, gc-only).
  • Not run: Linux/WSL, DefaultLib debug and JIT passes.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits October 26, 2026 15:07
A generator method whose state borrows `this` (@borrows("this")) no longer keeps it: the
signature pass finds the borrowing states before the facts, and the box's store of `this`
keeps nothing. Its callers keep their object and may use it after the loop.

A method of a class that is not @Final is called through its vtable: the forwarder check
(`entries() { return this.iter(); }`) and the bound check see the object through ts.GetThis.

A state field read only into a tuple yielded (`yield [k, v]`, also cast to a named tuple) moves
into the yield as a field read straight into it does, and a tuple made of borrowed strings
starts from copies of them (spec 22) instead of rc's retain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-on-some-paths, method callbacks

What the default library's Map and Set need to compile under own (spec
2026-10-26-own-generic-containers-design.md):

- A string read out of a place and given to a callback that may free it, or to a call that keeps it,
  gets a copy (released after the call when the callee does not keep it). A direct call that may
  free it is still an error, so @nodrops keeps its meaning.
- A parameter kept on some paths only is released on the others: takenParams is now the parameters
  a move reaches every return from.
- findBetween does not search past the read's own block: around a loop (`continue`) it is read again.
- A call's drops for `length =`, pop, shift, splice are the elements of arrays of that element type.
- Callback families for methods no other method overrides, called through their vtable; a closed
  family with no members calls nothing.

Tests: own_generator_method_this, own_string_copy_call, own_param_kept_some_paths,
own_method_callbacks, own_loop_read_again, own_length_drops_elements, own_tuple_param_kept,
own_err_generator_method_reassigned, own_err_callback_method_overridden. Retired:
own_err_param_kept_some_paths and own_err_string_call_keeps_twice, which now compile (and are
covered by the positive tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 491cc65 into main Oct 26, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-generator-this-borrows branch October 26, 2026 16:23
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