Skip to content

Commit

Permalink
fix #20152 Illegal capture of closure iterator, when should be legal (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 authored Oct 21, 2022
1 parent 84fab7f commit 66cbcaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/lambdalifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) =
else:
discard addField(obj, s, c.graph.cache, c.idgen)
# direct or indirect dependency:
elif (innerProc and s.typ.callConv == ccClosure) or interestingVar(s):
elif (innerProc and not s.isIterator and s.typ.callConv == ccClosure) or interestingVar(s):
discard """
proc outer() =
var x: int
Expand Down
20 changes: 20 additions & 0 deletions tests/closure/t20152.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
discard """
action: compile
"""

proc foo() =
iterator it():int {.closure.} =
yield 1
proc useIter() {.nimcall.} =
var iii = it # <-- illegal capture
doAssert iii() == 1
useIter()
foo()

proc foo2() =
proc bar() = # Local function, but not a closure, because no captures
echo "hi"
proc baz() {.nimcall.} = # Calls local function
bar()
baz()
foo2()

0 comments on commit 66cbcaa

Please sign in to comment.