Skip to content

-mm=own: an object literal whose arrows capture an object borrows it (spec 67) - #736

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

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

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

-mm=own: an object literal whose arrows capture an object borrows it (spec 67).

const b = new Box(5); const q = { w: (x: number) => x * b.n }; q.w(1); print(b.n) failed under own with "'this value' is used here after its value was moved". The literal's .captured box took ownership of b, while a plain arrow function capturing b only borrows it.

In every model, MLIRGen gives the literal no release, and rc gives the box no reference it later gives back. So the box only ever borrowed; own now says so:

  • Which literals: classifyLiteralCaptures picks out a capture box that is filled field by field and stored into the .captured field of the local the literal is built in, where that local is read once into the object ts.New made.
  • When: the literal must stay in the function. walkBorrowed from that object must find nothing that keeps it; calling its members and passing it to an object-type parameter (spec 66) are fine.
  • Then: each copied value that has its own owner (a named value released here, a parameter, or a read of a local) is stored as a borrow, the same attribute a closure's box uses. rc's retain before that store is erased. Uses of the literal count as uses of the borrowed value.

Test: own_literal_object_captures, under own, rc, gc and none. It captures a local object and a parameter, calls members, passes the literal to an object-type parameter, reads the objects afterwards, and churns memory between steps.

Not covered: a literal passed to an interface. The cast copies the literal into the interface's object, so the captured object stays moved into the box.

Validation: ctest 6324/6324. DefaultLib release JIT: gc 181/181, rc and none 180 (1 skipped), own 170 (11 skipped).

🤖 Generated with Claude Code

…(spec 67)

`const b = new Box(5); const q = { w: (x: number) => x * b.n }; q.w(1);
b.n` was "'this value' is used here after its value was moved": the
literal's `.captured` box took `b`, where a closure that does not escape
only borrows what it captures. The literal has no release, and rc no
reference of the box's own it gives back: the box only ever borrowed.

classifyLiteralCaptures: a capture box filled field by field and stored
once into the `.captured` field of the local a literal is built in, read
once into the object ts.New made. When walkBorrowed from that object
finds nothing that keeps it, each copy with an owner of its own is a
borrowing store (OWN_CAPTURE_BORROW_ATTR_NAME), rc's retain before it is
erased, and the literal's uses are the borrowed value's.

Test: own_literal_object_captures (own, rc, gc, none). A literal given to
an interface still moves the object (the cast copies the literal).

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