From 57f71b126e753ccfe67114d57531e2bb50045df8 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Tue, 27 Oct 2026 12:33:04 +0000 Subject: [PATCH] -mm=own: a for...of over an iterator read out of a place borrows it for...of desugars to `for (let .b = e, .c = .b.next(); ...)`, and under own a let initialized from a parameter is a second owner: `for (const v of iter)` over a parameter was "a parameter, kept here". .b is never assigned, so when e is a ts.Load it is now a const declared before the loop, a borrow the inference checks. Other models keep the counted let. Spec section 37. Co-Authored-By: Claude Opus 5.5 --- docs/own-memory-model.md | 5 + .../2026-09-24-own-memory-model-design.md | 28 +++++ tslang/lib/TypeScript/MLIRGenImpl.h | 20 +++- tslang/test/tester/CMakeLists.txt | 10 +- .../own/own_for_of_borrowed_iterator.ts | 105 ++++++++++++++++++ 5 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 tslang/test/tester/own/own_for_of_borrowed_iterator.ts diff --git a/docs/own-memory-model.md b/docs/own-memory-model.md index 00398a423..c0f04253d 100644 --- a/docs/own-memory-model.md +++ b/docs/own-memory-model.md @@ -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` 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` 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`. diff --git a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md index 7e0e2fd31..94a0f229a 100644 --- a/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md +++ b/tslang/docs/superpowers/specs/2026-09-24-own-memory-model-design.md @@ -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. \ No newline at end of file diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index 147a9cca2..33f38d2e4 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -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()) + { + NodeArray 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 nextArgs; diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index 38e495ea9..d8161560c 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -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. @@ -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) diff --git a/tslang/test/tester/own/own_for_of_borrowed_iterator.ts b/tslang/test/tester/own/own_for_of_borrowed_iterator.ts new file mode 100644 index 000000000..150a5a470 --- /dev/null +++ b/tslang/test/tester/own/own_for_of_borrowed_iterator.ts @@ -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(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(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(iter: TIter, other: TIter) { + let n = 0; + for (const v of iter) { + iter = other; + churn(); + n += v; + } + return n; +} + +// a field +class Holder { + 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."); +}