Skip to content

-mm=own: an object literal capturing an object, given to an interface, borrows it (spec 68) - #739

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-literal-interface-capture
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-literal-interface-capture

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Under -mm=own, an object literal whose arrows capture an object, passed to an interface parameter, moved the object into the literal:

const b = new Box();
use({ greet: () => "hi" + b.n }); // use(g: Greeter)
b.n; // error: used after its value was moved

The interface is a view of the literal's box (ts.NewInterface). When the interface is released, it frees the .captured box and every copy in it, so spec 67's borrow could not apply.

Spec 68

  • Signature pass (capturedBoxBorrows): .captured becomes one more field a borrowing record (spec 32) borrows. This happens only when:
    • each copy in the box has an owner of its own or holds no block;
    • no copy is a cell;
    • every interface made of the record is only retained, released, or given to calls that do not keep it.
  • Release (releaseFields): a borrowed capture-box field is freed with tsfrecb_, which frees the box but not what it holds.
  • Inference (classifyLiteralCaptures): the copies are bounded by the record's uses (walkRecord). classifyRecords leaves the .captured store alone.
  • checkBoundedResults now skips results that own no block. Before, a literal capturing a parameter bounded viaParam's number result, and print(viaParam(c)) was an error.

Test

New test own_literal_interface_captures, run under own, rc, gc and none, with churn between steps:

  • Borrows: literals capturing a local, two locals, a string beside an object, and a parameter.
  • Still takes copies: a literal that is returned as an interface, and one that is kept in an array.

Not covered (unchanged)

  • A literal capturing a let.
  • return applyO({ w: (x) => x + item.n }) with an object-type parameter.

Results

🤖 Generated with Claude Code

…n interface, borrows it (spec 68)

`use({ greet: () => "hi" + b.n }); b.n` for `use(g: Greeter)` was "used after its value was
moved": the interface's release freed the literal's `.captured` box with every copy in it.

- The signature pass marks `.captured` as a field a borrowing record (spec 32) borrows when each
  copy in the box has an owner of its own and nothing keeps or returns the interfaces.
- That record's release frees such a box alone (tsfrecb_).
- Inference bounds the copies by the record's uses (walkRecord) in place of walkBorrowed.
- checkBoundedResults skips results that own no block (a number bounded by a state-shaped literal).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit ec99210 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-literal-interface-capture branch October 29, 2026 16:27
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