Skip to content

Commit 66cbcaa

Browse files
authored
fix #20152 Illegal capture of closure iterator, when should be legal (#20607)
1 parent 84fab7f commit 66cbcaa

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/lambdalifting.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ proc detectCapturedVars(n: PNode; owner: PSym; c: var DetectionPass) =
458458
else:
459459
discard addField(obj, s, c.graph.cache, c.idgen)
460460
# direct or indirect dependency:
461-
elif (innerProc and s.typ.callConv == ccClosure) or interestingVar(s):
461+
elif (innerProc and not s.isIterator and s.typ.callConv == ccClosure) or interestingVar(s):
462462
discard """
463463
proc outer() =
464464
var x: int

tests/closure/t20152.nim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
discard """
2+
action: compile
3+
"""
4+
5+
proc foo() =
6+
iterator it():int {.closure.} =
7+
yield 1
8+
proc useIter() {.nimcall.} =
9+
var iii = it # <-- illegal capture
10+
doAssert iii() == 1
11+
useIter()
12+
foo()
13+
14+
proc foo2() =
15+
proc bar() = # Local function, but not a closure, because no captures
16+
echo "hi"
17+
proc baz() {.nimcall.} = # Calls local function
18+
bar()
19+
baz()
20+
foo2()

0 commit comments

Comments
 (0)