fix(codegen): a for loop over a plain iterator type iterates a copy - #145
Merged
Merged
Conversation
The language guide says a for loop always iterates a fresh copy of the value it started from, never the caller's own variable, and every generic direct implementor (Vec, Map, Set, user generics) did. A plain, non-generic type with its own Iterator impl drove the variable itself, so a loop advanced its cursor and a second loop over the same value, or a nested one, saw an exhausted iterator. It now copies the value into a fresh cursor first, for both the inlined and the call-based next().
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.
Summary
forloop always iterates a fresh copy of the value it started from, never the caller's own variable, so two loops over one value, or one nested in another, cannot corrupt each other's position. Every generic direct implementor (Vec,Map,Set, a user's own generic type) did that, but a plain, non-generic type with its ownIterator<T>impl drove the variable itself: afterfor x : c { ... },c.curhad advanced, and a second or nested loop overcsaw an exhausted iterator.__fun_iter_srcN) first and drives that, for both the inlinednext()and the call-based fallback, the same shape the generic path already uses.Counter*parameter.Test plan
fun test .33/33 filesfun -fmt-check-allclean,fun lint srcandfun lint stdlibcleanscripts/run_examples.sh218/218 (known pre-existinglet_and_lowlevel_types.fnflake)next()and for anelif-chainnext()that takes the call-based path, plus iteration through a pointer parameter