Skip to content

-mm=own: a for...of over an iterator read out of a place borrows it - #698

Merged
ASDAlexander77 merged 1 commit into
mainfrom
own-for-of-borrowed-iterator
Oct 27, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
own-for-of-borrowed-iterator

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Under -mm=own, for (const v of iter) over an iterator parameter was refused with "'iter' is a parameter, kept here, but -mm=own cannot make the callers of … give it up". Every default-library iterator helper that walks its source (forEach, every, some, find, toArray) failed this way.

Cause

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. The loop never assigns .b, so it only needs to borrow.

Change

mlirGenES2015: under own, when e is a ts.Load (a parameter, a variable, a field, a global), .b is 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.
  • gc, rc and none keep the let in every case. Its count keeps the iterator alive if the body assigns the variable it came from.

Checked against a compiler built without the change:

Case Result
A local let that the body reassigns main's IR is identical to before
A parameter that the body reassigns Compiles and prints the JavaScript result; assigning a parameter releases nothing, since the frame never owned it. Refused before
A labelled loop with continue outer / break outer Keeps its label: the const statement comes before the ts.For, not wrapped around it. Refused under own before

Spec section 37. docs/own-memory-model.md lists what's still open for the lazy helpers, with two workarounds, both tested.

Tests

  • own_for_of_borrowed_iterator churns between steps and covers:

    • a parameter;
    • a labelled loop;
    • a parameter the body reassigns;
    • a field;
    • a global.

    It runs under own, rc, gc and none, and own emits no counting. Without the change, own rejects it with 4 errors.

  • Full suite: 5860 passed, 0 failed.

  • DefaultLib, library rebuilt and each model run in its own process:

    • own: 132 → 137 of 170. iterator_every, find, forEach, some and toArray pass, with no new failures.
    • gc, rc and none: all pass.

Not here

  • map, filter, take, drop: they still give a result that borrows its source, so const seq = fibonacci().map(f) is an error. That needs a generator state that owns a parameter.
  • reduce with a string accumulator: its let starts from the initial parameter.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 1be4740 into main Oct 27, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the own-for-of-borrowed-iterator branch October 27, 2026 12:56
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