iterinliner: rename captured locals inside closures in a copied for body - #2575
Merged
Araq merged 1 commit intoSep 23, 2026
Merged
Conversation
… body
When an inline iterator is expanded, each `yield` gets a copy of the `for`
body with fresh names for the locals and labels it declares
(`collectDefs` + `copyFreshened`). `copyFreshened` copied nested routines
verbatim, so a closure declared in the body kept referring to the body's
locals by their OLD names; the declarations were renamed away and a later
pass hit `[Bug] could not find symbol: v.0`:
proc populate(n: int): int =
result = 0
for d in 0 ..< n:
var v = d
proc fill(): int {.closure.} = v
result += fill()
Nested routines are now copied through the same renaming, which touches
only the enclosing body's names (the mapping holds nothing the routine
declares itself). Each copy also gets its own name for the routine, so an
iterator with several yields does not define one closure symbol per copy
under a single name. The same path serves `expandInlineIterator`, so a
closure inside an inline iterator's body capturing the iterator's own
locals is fixed too. Templates, macros, types and pragmas are still copied
verbatim.
Found porting sumi (a closure over pNext-chained locals inside a
`for d in devices:` loop). `hastur all`: 903/903.
Co-Authored-By: Claude Opus 5.5 (1M context) <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.
When an inline iterator is expanded, each
yieldgets a copy of theforbody with fresh names for the locals and labels it declares (collectDefs+copyFreshened).copyFreshenedcopied nested routines verbatim, so a closure declared in the body kept referring to the body's locals by their OLD names; the declarations were renamed away and a later pass hit[Bug] could not find symbol: v.0:Nested routines are now copied through the same renaming, which touches only the enclosing body's names (the mapping holds nothing the routine declares itself). Each copy also gets its own name for the routine, so an iterator with several yields does not define one closure symbol per copy under a single name. The same path serves
expandInlineIterator, so a closure inside an inline iterator's body capturing the iterator's own locals is fixed too. Templates, macros, types and pragmas are still copied verbatim.