Skip to content

Commit

Permalink
fixes #15924; Tuple destructuring is broken with closure iterators (#…
Browse files Browse the repository at this point in the history
…23205)

fixes #15924

(cherry picked from commit 8484abc)
  • Loading branch information
ringabout authored and narimiran committed Apr 20, 2024
1 parent fbb9ce4 commit d57200b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
13 changes: 8 additions & 5 deletions compiler/lambdalifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -989,12 +989,15 @@ proc liftForLoop*(g: ModuleGraph; body: PNode; idgen: IdGenerator; owner: PSym):
# gather vars in a tuple:
var v2 = newNodeI(nkLetSection, body.info)
var vpart = newNodeI(if body.len == 3: nkIdentDefs else: nkVarTuple, body.info)
for i in 0..<body.len-2:
if body[i].kind == nkSym:
body[i].sym.transitionToLet()
vpart.add body[i]
if body.len == 3 and body[0].kind == nkVarTuple:
vpart = body[0] # fixes for (i,j) in walk() # bug #15924
else:
for i in 0..<body.len-2:
if body[i].kind == nkSym:
body[i].sym.transitionToLet()
vpart.add body[i]

vpart.add newNodeI(nkEmpty, body.info) # no explicit type
vpart.add newNodeI(nkEmpty, body.info) # no explicit type
if not env.isNil:
call[0] = makeClosure(g, idgen, call[0].sym, env.newSymNode, body.info)
vpart.add call
Expand Down
18 changes: 18 additions & 0 deletions tests/iter/titer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,21 @@ block: # bug #21110
e()
static: foo()
foo()


# bug #15924
iterator walk(): (int, int) {.closure.} =
yield (10,11)

for (i,j) in walk():
doAssert i == 10

proc main123() =
let x = false
iterator it(): (bool, bool) {.closure.} = # normally {.closure.} here makes #21476 work
discard x

for (_, _) in it():
discard

main123()

0 comments on commit d57200b

Please sign in to comment.