Repository navigation
-mm=own: a for...of over an iterator read out of a place borrows it - #698
Merged
Merged
Conversation
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>
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.
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 tofor (let .b = e, .c = .b.next(); !.c.done; .c = .b.next()). Under own, aletinitialized from a parameter is a second owner of it. The loop never assigns.b, so it only needs to borrow.Change
mlirGenES2015: under own, wheneis ats.Load(a parameter, a variable, a field, a global),.bis aconstdeclared just before the loop, in the same scope. It names the value, a borrow the inference checks likeconst it = iter.e(for (const v of gen())) keeps thelet, which owns it.letin 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:
letthat the body reassignsmain's IR is identical to beforecontinue outer/break outerconststatement comes before thets.For, not wrapped around it. Refused under own beforeSpec section 37.
docs/own-memory-model.mdlists what's still open for the lazy helpers, with two workarounds, both tested.Tests
own_for_of_borrowed_iteratorchurns between steps and covers: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:
iterator_every,find,forEach,someandtoArraypass, with no new failures.Not here
map,filter,take,drop: they still give a result that borrows its source, soconst seq = fibonacci().map(f)is an error. That needs a generator state that owns a parameter.reducewith a string accumulator: itsletstarts from theinitialparameter.🤖 Generated with Claude Code