Repository navigation
-mm=own: a field that refers back to its own object borrows it (spec 70) - #745
Merged
Merged
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Own spec §70 (approved): a field that refers back to its own object.
class A { n = 1; fn = () => this.n; }failed under-mm=ownwith "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.getfailed 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.thisis captured by copy, in every model (MLIRGen).thisis 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 readthis. rc keeps the same cycle it had with the cell.A back-reference is a closure whose box holds the member's
thisand nothing else that owns a block, stored once into a field of thatthis. Under own:__own_borrows_captures, so the box frees only itself;A bound method of
thisstored into a field ofthisalready borrowed, since releasing it frees nothing of the object. Own now proves that.thisin any capture box keeps nothing. The signature pass no longer counts storingthisinto a box as keeping it, so inference decides each closure:this, as it borrowed the cell before (own_closure_borrow,own_raytrace'saddLight);this(return () => this.n) is still an error.Fields that hold back-references are listed in the
ts.own_back_reference_fieldsmodule attribute.checkPlaceReadnever takes a value out of such a field. Soreturn a.fnandh.call = a.fnstay 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:own_err_back_reference_taken,own_err_back_reference_storedandown_err_back_reference_captures.Not covered
fn = () => this.propwithget prop()): unchanged, fails in every model.--no-default-libown error when calling a field that returns a string, whatever the field holds.🤖 Generated with Claude Code