Skip to content

-mm=own: a field that refers back to its own object borrows it (spec 70) - #745

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-back-references
Oct 29, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-back-references

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Own spec §70 (approved): a field that refers back to its own object.

class A { n = 1; fn = () => this.n; } failed under -mm=own with "a closure that escapes owns what it captures, but -mm=own cannot move this variable into it: its value is the caller's". fn = this.get failed with "'this value' takes a second reference". Owning the field's value would make a cycle: the object owns the field, the field owns the closure's box, and the box owns the object. Such a value now borrows its object. The field owns the closure, and releasing the box frees the box but not the object.

this is captured by copy, in every model (MLIRGen). this is never assigned, so a copy is the same object, and the closure no longer needs a cell for it. This changes the code gc and rc generate for arrows that read this. rc keeps the same cycle it had with the cell.

A back-reference is a closure whose box holds the member's this and nothing else that owns a block, stored once into a field of that this. Under own:

  • its copies are borrowed;
  • the closure gets __own_borrows_captures, so the box frees only itself;
  • rc's retain of the copy goes.

A bound method of this stored into a field of this already borrowed, since releasing it frees nothing of the object. Own now proves that.

this in any capture box keeps nothing. The signature pass no longer counts storing this into a box as keeping it, so inference decides each closure:

  • one that doesn't escape borrows this, as it borrowed the cell before (own_closure_borrow, own_raytrace's addLight);
  • a back-reference borrows it as above;
  • any other escaping closure over this (return () => this.n) is still an error.

Fields that hold back-references are listed in the ts.own_back_reference_fields module attribute. checkPlaceRead never takes a value out of such a field. So return a.fn and h.call = a.fn stay borrows of the object and give the existing error, "borrows a field and cannot be stored, returned or captured".

Calling a back-reference that rewrites its own field is safe. A closure body reads its by-copy captures once, at entry, so the box is never read after it's freed. The error the proposal planned for this isn't needed.

Tests

  • own_back_reference, under own, rc, gc and none, plus own's no-counting check. It covers:
    • arrow fields: one with a parameter, one calling another arrow field and a method;
    • a bound method field;
    • fields reassigned in a method and from outside;
    • a derived class's fields;
    • calls after the object moves, and on objects an array holds.
  • Error tests: own_err_back_reference_taken, own_err_back_reference_stored and own_err_back_reference_captures.
  • 2 million objects with an arrow field each run in a flat 49 MB under own.
  • ctest 6408/6408. DefaultLib: gc 182/182, rc 181, none 181, own 171 (11 skipped by design).

Not covered

  • An arrow field that reads a getter (fn = () => this.prop with get prop()): unchanged, fails in every model.
  • Back-references in an array the object holds, or in a nested object's field: out of scope (Q3).
  • An existing --no-default-lib own error when calling a field that returns a string, whatever the field holds.

🤖 Generated with Claude Code

`class A { n = 1; fn = () => this.n; }` was "a closure that escapes owns what it captures, but -mm=own
cannot move this variable into it: its value is the caller's", and `fn = this.get` was "'this value'
takes a second reference": the object would own its field, the field the closure's box, the box the
object.

- MLIRGen, every model: a class member's `this` is captured by copy (setCaptureByValue, as a @value
  class's was). It is never assigned, so the copy is the same object, and there is no cell.
- A back-reference's box (isBackReferenceBox): it holds the member's `this` and nothing else that owns
  a block, and the closure is stored once into a field of that `this`. Its copies borrow, the closure
  is `__own_borrows_captures` (the box frees itself alone), and rc's retain goes.
- A bound method of `this` stored into a field of `this` already borrows (its release frees nothing of
  the object): the store keeps nothing and rc's retain goes.
- `this` in any capture box keeps nothing (the signature pass's `keeps`). A closure that does not
  escape borrows it, as it borrowed the cell; a copies-only one when it holds `this` and nothing made
  for it. Any other escaping closure over `this` stays an error.
- The fields back-references are stored into (`ts.own_back_reference_fields`): checkPlaceRead takes
  nothing out of them, so `return a.fn` and `h.call = a.fn` stay borrows of the object, and errors.
- own_back_reference (own, rc, gc, none; no counting); own_err_back_reference_{taken,stored,captures}.
  The spec's §70 describes it, with the user's decisions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit cce1166 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-back-references branch October 29, 2026 23:48
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