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
5 changes: 5 additions & 0 deletions docs/own-memory-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ a reference gives it back on every path out of a function.
- Array methods that keep elements of the source in a new array (`filter` over class instances)
give each such element a second owner, which is an error by design. Use `Shared<T>` elements,
or build the result from copies.
- Iterator helpers that walk their source (`forEach`, `every`, `some`, `find`, `toArray`) work.
The lazy ones (`map`, `filter`, `take`, `drop`) give an iterator that borrows its source, so
`const seq = fibonacci().map(f)` is an error: use it in place (`for (const v of
fibonacci().map(f))`), or, in a function, name the source first (`const src = fibonacci();
const seq = src.map(f);`). A module-level `const` is a global, and a global cannot hold a borrow.
- `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
Original file line number Diff line number Diff line change
Expand Up @@ -4508,3 +4508,31 @@ Not here:
- `const ts: Th[] = [new A(), new B()]; const h = ts[1].hit(ray); print(h.dist)` outside a loop is
`borrows an element but is used here after it may be released or overwritten` for a record with a
box (section 32) as much as for one without; the same call inside a loop over `ts` compiles.

## 37. A `for...of` over an iterator read out of a place borrows it, 2026-10-27

MLIRGen desugars `for (const v of e)` over an iterator to `for (let .b = e, .c = .b.next(); !.c.done;
.c = .b.next())`. Under own a `let` initialized from a parameter is a second owner of it, so
`function forEach(iter: TIter, f) { for (const v of iter) f(v); }` was "'iter' is a parameter, kept
here", and so was every iterator helper of the default library that walks its source (`forEach`,
`every`, `some`, `find`, `toArray`). `.b` is never assigned. Under own, when `e` is a `ts.Load` - a
parameter, a variable, a field, a global - `.b` is now a `const` declared just before the loop, in the
same scope: it names the value, a borrow the inference checks like `const it = iter`. A fresh `e`
(`for (const v of gen())`) keeps the `let`, which owns it. The other models keep the `let` in every
case: its count keeps the iterator alive should the body assign the variable it came from.

A body that assigns the variable is safe under own: a parameter's slot releases nothing when assigned
(the frame never owned it), and a local `let`'s loop compiles to the same IR as before (`.b` gets a
slot, and the inference moves the value into it as it did the `let`). A labelled loop keeps its label:
the `const` statement is emitted before the `ts.For`, not wrapped around it.

`own_for_of_borrowed_iterator` runs a parameter, a labelled loop with `continue outer` and `break
outer`, a parameter the body assigns, a field and a global, churning between steps, under own, rc,
gc and none; own emits no counting. Before the change own rejected it with four errors. DefaultLib
under own: `iterator_every`, `iterator_find`, `iterator_forEach`, `iterator_some` and
`iterator_toArray` compile and pass.

Not here: the lazy helpers `map`, `filter`, `take` (generators whose state borrows the source) and
`drop` (which returns it) still give a result that borrows a temporary, so `const seq =
fibonacci().map(f)` is an error; that needs a generator state that owns a parameter. `reduce` with a
string accumulator starts its `let` from the `initial` parameter.
20 changes: 19 additions & 1 deletion tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5154,7 +5154,25 @@ class MLIRGenImpl
auto _b = nf.createIdentifier(S(".b"));
auto _next = nf.createIdentifier(S(ITERATOR_NEXT));
auto _bVar = nf.createVariableDeclaration(_b, undefined, undefined, nf.createIdentifier(S(EXPR_TEMPVAR_NAME)));
declarations.push_back(_bVar);
// -mm=own: an iterator read out of a place - a parameter, a variable, a field - is lent to the
// loop, which never assigns `.b`. As a `let` it was a second owner: `for (const v of iter)`
// kept the parameter `iter`. A `const` names the same value, a borrow the inference checks.
// Under the other models the `let`'s count keeps the iterator alive should the loop assign the
// variable it came from.
if (compileOptions.memoryModel == MemoryModelOwn && exprValue.getDefiningOp<mlir_ts::LoadOp>())
{
NodeArray<VariableDeclaration> borrowed;
borrowed.push_back(_bVar);
if (mlir::failed(mlirGen(nf.createVariableStatement(undefined, nf.createVariableDeclarationList(borrowed, NodeFlags::Const)),
genContext)))
{
return mlir::failure();
}
}
else
{
declarations.push_back(_bVar);
}

NodeArray<Expression> nextArgs;

Expand Down
10 changes: 9 additions & 1 deletion tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,8 @@ foreach(own_test own_fresh_string own_fresh_array own_return_new own_try_local o
own_tuple_record_values own_value_record own_value_loop own_value_temporaries
own_call_field_drops own_immortal_keepers
own_generator_method_this own_string_copy_call own_param_kept_some_paths own_method_callbacks
own_loop_read_again own_length_drops_elements own_tuple_param_kept)
own_loop_read_again own_length_drops_elements own_tuple_param_kept
own_for_of_borrowed_iterator)
tslang_add_test(NAME test-jit-own-${own_test} COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
tslang_add_test(NAME test-compile-own-${own_test} COMMAND test-runner -mm=own "${PROJECT_SOURCE_DIR}/test/tester/own/${own_test}.ts")
# The promise: no counting at all in what own emits.
Expand All @@ -3956,6 +3957,13 @@ foreach(immortal_mm rc gc none)
endforeach()
endforeach()

# A for...of over an iterator read out of a place borrows it under own; the same program under every
# other model, where its cursor stays a counted `let`.
foreach(for_of_mm rc gc none)
tslang_add_test(NAME test-jit-${for_of_mm}-own_for_of_borrowed_iterator COMMAND test-runner -jit -mm=${for_of_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_for_of_borrowed_iterator.ts")
tslang_add_test(NAME test-compile-${for_of_mm}-own_for_of_borrowed_iterator COMMAND test-runner -mm=${for_of_mm} "${PROJECT_SOURCE_DIR}/test/tester/own/own_for_of_borrowed_iterator.ts")
endforeach()

# A value read out of an assigned global, lent to a call that assigns only other globals, outlives the
# call; own refuses a call that may assign that global or one it cannot name.
foreach(other_global_mm own rc gc none)
Expand Down
105 changes: 105 additions & 0 deletions tslang/test/tester/own/own_for_of_borrowed_iterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// A `for...of` over an iterator read out of a place - a parameter, a field, a global - lends it to the
// loop, which never assigns its hidden cursor. Under -mm=own the cursor was a `let`, a second owner, so
// `for (const v of iter)` over a parameter was "a parameter, kept here". Each case churns between
// steps, so an iterator freed early reads garbage.
class C {
v: number[] = [];
constructor(public x: number) {}
}

function churn() {
const junk: C[] = [];
for (let i = 0; i < 64; i++) {
const c = new C(999);
c.v.push(99);
junk.push(c);
}
}

function* gen() {
yield 1;
yield 2;
yield 3;
yield 4;
}

type It = { next(): { value: any, done: boolean } };

// a parameter
function sum<TIter extends It>(iter: TIter) {
let n = 0;
for (const v of iter) {
churn();
n += v;
}
return n;
}

// a parameter, through `continue` and `break` of a label
function labelled<TIter extends It>(iter: TIter) {
let s = "";
outer: for (const v of iter) {
for (let j = 0; j < 3; j++) {
churn();
if (v == 2) continue outer;
if (v == 4) break outer;
if (j == 1) break;
s += v;
}
s += ";";
}
return s;
}

// a parameter the loop assigns: the cursor keeps the first, which is still the caller's
function reassigns<TIter extends It>(iter: TIter, other: TIter) {
let n = 0;
for (const v of iter) {
iter = other;
churn();
n += v;
}
return n;
}

// a field
class Holder<T extends It> {
constructor(public it: T) {}

sum() {
let n = 0;
for (const v of this.it) {
churn();
n += v;
}
return n;
}
}

// a global
const g = gen();

function sumGlobal() {
let n = 0;
for (const v of g) {
churn();
n += v;
}
return n;
}

function main() {
let t = 0;
for (let i = 0; i < 1000; i++) {
t += sum(gen());
const it = gen();
t += sum(it);
assert(labelled(gen()) == "1;3;", "labelled");
t += reassigns(gen(), gen());
t += new Holder(gen()).sum();
}

t += sumGlobal();
assert(t == 40010, "t");
print("done.");
}
Loading