Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/own-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -896,10 +896,15 @@ a reference gives it back on every path out of a function.
this.head = n`) or at the tail (`c.next = n`), and `this.head = this.head.next` or `c.next =
c.next.next` takes a node out. The node taken out is freed right there, so read what you need of
it first: `const h = this.head; const v = h.v; this.head = h.next; return v;`, not `return h.v`
after the assignment. Not accepted yet: a node made with the old head as an argument (`this.head =
new Node(v, this.head)`), a walk that assigns a parameter (`n = n.next`), and an insert that
recurses on a parameter and gives it back (`t.left = insert(t.left, v)`); write the last as a
method that recurses on `this`.
after the assignment. A node can also be made with the old head as an argument (`this.head = new
Node(v, this.head)`), and a tree grown by a function that recurses on a field and gives it back
(`t.left = insert(t.left, v)`): the field is given up to the call, and its result refills it. That
takes a call to a function of the same module that never throws (no `throw` and no `await` in it or
in anything it calls), whose other arguments are values with no heap block or the object `new` made;
anything else is an error. A walk may assign its
parameter (`while (n) { k++; n = n.next; }`) when every value it gives it is read out of the one it
holds: the parameter then borrows the caller's list. A parameter given anything else (`n = new
Node(0)` on some path) is kept, and the caller gives it up.
- `Shared<T>` cycles are never freed.
- There are no lifetime annotations: a borrow that the compiler cannot follow is an error, even
when the program would be correct. The fix is usually a move, a fresh value or `Shared<T>`.
Expand Down
18 changes: 11 additions & 7 deletions docs/own-readiness.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ This is weak evidence, for three reasons. Half of what the fuzzer generates is r
350 programs ran. It was Windows only, and the Windows heap hides double frees that glibc reports.
And compiling one real codebase turned up, between 2026-11-13 and 2026-11-15, a read of freed
memory (item 50), a double free (item 83), a fix that leaked 2.7 GB and was backed out (item 80),
and a compiler crash in about one run in 500 (#867). All are fixed. The rate says more are there.
and a compiler crash in about one run in 500 (#867). All are fixed. The rate says more are there:
the change that added this page accepted a walk that cut the list ahead of itself (`const nx =
n.next; n.next = null; n = nx`) and read the freed node, found the same day by a probe written for
the next change (spec 83).

## Coverage

Expand Down Expand Up @@ -96,9 +99,10 @@ Probed one by one under the JIT:
| Appending at the tail, cutting the tail: `c.next = n`, `c.next = null` after the walk | Compiles and runs |
| List `pop`: `this.head = this.head.next`; unlinking: `c.next = c.next.next` | Compiles and runs |
| Using the popped node afterwards: `this.head = h.next; return h.v` | Rejected: the node is freed by the assignment; read it first |
| Push through a constructor: `this.head = new Node(v, this.head)` | Rejected |
| Walking by assigning a parameter: `n = n.next` | Rejected |
| Tree insert by recursion on a parameter: `t.l = ins(t.l, v)` | Rejected |
| Push through a constructor: `this.head = new Node(v, this.head)` | Compiles and runs, when the constructor is in the same module and never throws |
| Walking by assigning a parameter: `n = n.next` | Compiles and runs |
| Tree insert by recursion on a parameter: `t.l = ins(t.l, v)` | Compiles and runs, when the function is in the same module and never throws |
| Cutting while walking: `const nx = n.next; n.next = null; n = nx` | Rejected: the cut frees what `nx` names (accepted by mistake from 2026-11-16 until spec 83) |
| Tree insert and sum as methods recursing on `this` | Compiles and runs |
| Parent link: `k.parent = this` | Rejected; the answer is `Shared<T>`, whose cycles leak |
| `new WeakRef(a)` | Rejected: built on the collector |
Expand Down Expand Up @@ -146,9 +150,9 @@ assertions-enabled CI leg, and a soak run. The first holds today; the other two

1. **A non-owning back-reference.** `WeakRef<T>` under own, or a declared borrowing field. Without
it a tree with parent links either does not compile or leaks.
2. **The rest of linked structures.** A list now works; a node built from the old head, a walk
that assigns a parameter and the recursive tree insert do not. These are what a newcomer writes
first.
2. **The rest of linked structures.** A list, a node built from the old head, a walk that assigns a
parameter and the recursive tree insert work (spec 83). A call that may throw, or one into another
module, still cannot take a field this way, and the popped node cannot be used after the assignment that frees it.
3. **The "not yet" messages.** Second references the compiler cannot classify, values merged from
branches, object literals that hold owned values.
4. **Capturing callbacks over `Map` and `Set`**, and a source position for the error they give.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5850,3 +5850,64 @@ Tests: `00linked_list` and `00linked_list_kept` (own, rc, gc, none; also `-noopt
`own_err_list_walk_dropped`, `_walk_call_drops`, `_lookahead_cut`, `_walk_up`, `_pop_used_after`,
`_next_kept_twice`, `_refill_from_param`. The corpus, the mm fuzzer (1,000 seeds) and native_ts_graphics'
own build are unchanged by it: no file newly refused.

## 83. A field given to a call and refilled; a parameter that walks; a walk cut ahead of itself, 2026-11-16

Three of the shapes section 82 left refused, and a hole it opened.

**A field given to a call that keeps it, refilled from the call's result (`calledFieldTakeOf`).**
`this.head = new Node(v, this.head)` and `t.left = insert(t.left, v)`: the read of the field goes to a
call that keeps that argument (`__own_params`), and the store right after it puts the call's result (a
constructor's: the object `new` made) back into the same field of the same object. Section 82's moves
out of a field all have nothing but reads between the read and the refill; here the callee runs, and
it may reach the field another way (`insert` carries `__own_drop_fields` for the very field it is
given). So the field is given up before the call (`ts.TakeOutOfField`, as for a splice: emptied in a
mortal object; kept, with its value made immortal, in one an append-only global keeps), and the
refill's release stays: it frees what the field holds then, nothing unless the callee put something
there. The match: a class field holding a class object or null (not a string, a handle, a
back-reference or a borrowing field); the read given once, to that call only, in its block; between
the read and the call only reads, constants and views; between the call and the refill only views of
the result and rc's retain of them. The call's other arguments hold no heap block, or are the object
`new` made: not the object the field is in (`this.wrap(this.head)`, `own_err_list_push_through_this`)
nor the field again (`f(t.l, t.l)`, `own_err_tree_field_given_twice`).

The call must never throw (`__own_no_throw`, below). A throw caught by any caller would find the field
given up where gc and rc keep the old value: `this.head = new Node(v, this.head)` with a constructor
that throws would empty the list (`own_err_list_push_ctor_throws`). Not an invoke either, whose normal
path goes on in another block.

**`__own_no_throw` (`OwnershipSignaturePass::computeNoThrow`).** A greatest fixpoint over the module: a
function never throws while its body holds no `ts.Throw`, `ts.ThrowCall` or `ts.ThrowUnwind` (a failed
cast is a `ts.Throw` MLIRGen writes into the body) and no op of the `async` dialect (an await rethrows),
and every call in it is resolved to functions of this module that never throw. An imported, unresolved
or `instanceof` call may throw, so a constructor of another module's class cannot take a field. The fact is put on
each call, since inference reads no other function's body (#867).

**A parameter that only walks down borrows (`walksDownOnly`).** Section 81 made an assigned parameter a
kept one: its local owns what it holds. A local whose every assignment stores a value read out of what
it holds, through fields of class objects (`n = n.next`, `n = n.left.right`; not through a handle),
owns none of it: it walks down from what it was given. Any other use of the local but reads, rc's counts
and the debugger's record (a swap, its address given away) keeps the old rule. `keeps` no longer counts such a local as the
parameter's taker, so the parameter stays a borrow, and inference treats the local as section 82's
`let` given only borrows. One assignment of anything else (`n = new Node(9)` on some path) keeps the
old rule (`own_err_param_walk_mixed`). The same holds for `let n = m` of a parameter `m`.

**A walk cut ahead of itself (the hole).** Section 82's `frees` let a release of a field of what the
walking `let` holds go when the `let` only walks down, reading "what the `let` holds" as the value read
or one under it. That is true only once the `let` has been given the value read. `const nx = n.next;
n.next = null; n = nx` reads `n` for the cut while it still holds the node above: the release frees the
node `nx` names, and the walk goes on into it. Section 82 accepted it, and the program crashed
(`own_err_list_unlink_walk`, refused before section 82). Now the read of the `let` that the released
place hangs from must come after the assignment: every way from the read to it runs the assignment,
and no way from it reaches the drop through the read again. `c.next = (c = c.next, null)` reads `c`
for the place before the step and stores after it: section 82 accepted it and the program crashed
(`own_err_list_cut_in_step`). The cut after the walk (`if (c) c.next = null`) and a
cut of the node just stepped to (`c = c.next; if (c && c.v == 2) c.next = null`) still compile.

Still refused: a field given to a call that may throw, a node used after the assignment that frees
it, and a walk that keeps or returns the parameter it walks (`last(n)` returning `n`: the local borrows,
and a borrow is not returned from a parameter's local).

Tests: `00linked_list_build` (own, rc, gc, none; also `-noopt` under own), and
`own_err_list_unlink_walk`, `_cut_in_step`, `_cut_through_alias`, `_push_ctor_throws`, `_push_through_this`,
`own_err_tree_field_given_twice`, `own_err_param_walk_mixed`.
8 changes: 8 additions & 0 deletions tslang/lib/TypeScript/OwnershipFacts.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ namespace mlir_ts = mlir::typescript;
// The callee destroys nothing its caller can reach: no overwrite of a place it did not make, no
// assignment of a global, no call that may.
#define OWN_NO_DROPS_ATTR_NAME "__own_no_drops"
// The callee never throws: no `throw` in its body or in any function it calls, every call it makes
// resolved to functions of this module (OwnershipSignaturePass::computeNoThrow).
#define OWN_NO_THROW_ATTR_NAME "__own_no_throw"
// The callee destroys nothing its caller can reach but the old values of the places it overwrites:
// [position, slot type] pairs - a field's position (OWN_INTERFACE_FIELD_POSITION for a field reached
// through an interface), -1 for an element - judged as those stores would be if they were the
Expand Down Expand Up @@ -204,6 +207,11 @@ inline bool callNoDrops(mlir::Operation *op)
return op->hasAttr(OWN_NO_DROPS_ATTR_NAME);
}

inline bool callNoThrow(mlir::Operation *op)
{
return op->hasAttr(OWN_NO_THROW_ATTR_NAME);
}

// The places a call's callees may overwrite (OWN_DROP_FIELDS_ATTR_NAME), when that is all they
// destroy; none when they may destroy anything.
inline std::optional<llvm::SmallVector<std::pair<int64_t, mlir::Type>>> callDropFields(mlir::Operation *op)
Expand Down
9 changes: 9 additions & 0 deletions tslang/lib/TypeScript/OwnershipInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,15 @@ class OwnershipInferencePass
// the field is emptied right before the release, and its value is the place's. `take.release` is
// that release.
bool splicedFieldTakeOf(mlir::Operation *read, FieldTake &take);
// ...or is `kept` a call that keeps `read`, a read of a field of a class object, whose result is
// stored into that same field right after it: `this.head = new Node(v, this.head)`, `t.left =
// insert(t.left, v)`. The field is given up right before the call (ts.TakeOutOfField), so that
// nothing the callee does through another way to the object sees the value twice, and the refill's
// release frees what the field holds then. `take.release` is that release.
bool calledFieldTakeOf(mlir::Operation *read, mlir::Operation *kept, FieldTake &take);
// Are `object` and `other` the same object: sameObject, or two reads of one local in a block
// with no assignment of it and nothing that runs other code between them?
static bool sameObjectInBlock(mlir::Value object, mlir::Value other);
// Can `call` be given a copy of `value`, a string read out of a place, in its place (callCopies)?
static bool copiesInto(mlir::Operation *call, mlir::Value value);
// Does `store` put `value`, a string read out of a place, into another place with no retain of
Expand Down
Loading
Loading