Skip to content

-mm=own: callbacks declaring fewer parameters over Map/Set; WeakRef of a nullable target - #872

Merged
ASDAlexander77 merged 4 commits into
mainfrom
own-callbacks-and-gaps
Nov 16, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
own-callbacks-and-gaps

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Readiness item 4, plus the first deferred minor of the WeakRef review (#871) and the readiness docs.

Capturing callbacks over Map and Set

Under own, this failed with an error at <unknown>:0:

m.forEach((v, k) => { names.push(k); });

The callback declares fewer parameters than forEach's type ((value, key, map) => void), so the compiler calls it through a generated adapter (fewerParametersThunk, <callee>.args<N>). The adapter passed its block arguments straight to the callback. Own therefore never saw it keep what its callee keeps, and it reported a value given without being owned. The adapter also had no source location. A callback that declared every parameter already compiled.

  • Parameters through slots. The adapter now takes its parameters into slots (ts.Param), as any function does. It keeps what its callee keeps, and the callback family decides it like any other member (spec 33).
  • Copies when callbacks disagree. When the callbacks passed in its place disagree (one keeps the key, another the value), their facts are dropped. A string the adapter then only borrows gets a copy for the callee that keeps it (callCopies, spec 22), as a string read out of a field does.
  • A real location. Without --di the adapter now carries the location it is made at, so an error found in it points at the callback's use.

A note on the first version of the copy: it was recorded in the dry run but never made, because the dry run then passed. Two disagreeing callbacks over one Map gave the Map's own key away, and it was freed twice. My compiler test missed this because string literals are immortal. The new default-library test, with keys built at run time and a later delete, crashed under own. The second commit fixes it, and own_callback_fewer_params now builds its strings at run time and frees the table's strings before checking.

WeakRef of a nullable declared target

let n: Node | null = new Node(); new WeakRef(n) failed under every model:

  • own and none refused it ("WeakRef needs a class object");
  • gc built a WeakRef<Node | null> that a later store into WeakRef<Node> | string failed to verify.

With no type argument, T is now the target's type without null. A null target makes a WeakRef whose deref() gives undefined; the copy, the release and deref already test for null.

Docs

docs/own-readiness.md was stale after #871 and this change. I updated:

  • the verdict, the scorecard and the "ready for" table;
  • the Map/Set callback row and the exit-criteria row (now half met: optional accessor fields remain);
  • the work list (item 4 removed).

docs/production-readiness-plan.md is updated too, and spec section 85 records both changes.

Validation

Check Result
Windows ctest 7584/7584
Linux own, shared, callback and WeakRef tests (MALLOC_PERTURB_=165, tcache off) 2728/2728
Differential fuzzer, seed 919000 ×500 936 programs, 0 failures
Corpus sweep against main only the two new tests change (refused → accepted); no other file differs, no crash
Default library under own, gc, none and rc everything passes; the only tests not run are those skipped by design

The default-library run includes the companion PR's new test. That test first failed only because it ended with done. instead of the ALL DONE marker; after the fix it passes under all four models (JIT).

Companion: a default-library test of Map and Set forEach callbacks that keep their keys and values. Merge this PR first.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits November 16, 2026 13:46
`m.forEach((v, k) => { names.push(k); })` over a Map or a Set failed under own with
"argument 'this value' is given to '...', which keeps it, but -mm=own cannot move it
here", at <unknown>:0. The callback declares fewer parameters than forEach's type, so
it is called through an adapter (fewerParametersThunk) that passed its block arguments
straight on: own saw an argument the adapter did not own given to a callee that keeps it.

The adapter now takes its parameters into slots, as any function does, so own reads it
as keeping what its callee keeps; when callbacks passed in its place disagree, a string
it only borrows is copied for the callee that keeps it (callCopies, spec 22), as one read
out of a field is. Without --di it carries the location it is made at, so an error found
in it points at the callback's use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`let n: Node | null = new Node(); new WeakRef(n)` was refused under own and none
("WeakRef needs a class object"), and under gc made a WeakRef<Node | null> that a later
store into `WeakRef<Node> | string` failed to verify. TypeScript reads n as Node there;
this compiler keeps the declared type, so T is now the target's type without null. A null
target makes a WeakRef whose deref() gives undefined.

Deferred minor M1 of the WeakRef review (#871).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…here)

The copy 3da319c meant for a string an adapter only borrows was recorded in the dry
run, which then passed, so the copies were never made: the adapter gave the Map's own
key to a callback that pushed it, and the key was freed twice (two forEach callbacks over
one Map, keeping the key and the value, crashed in a loop). The dry run now fails there,
as for a string read out of a place, and the real run finds the copy given.

own_callback_fewer_params now builds its strings at run time and frees the table's after
the callbacks: literals are immortal, so the test passed without the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 7c843a2 into main Nov 16, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-callbacks-and-gaps branch November 16, 2026 14:36
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