fixes #15924; Tuple destructuring is broken with closure iterators (#23205)

fixes #15924
This commit is contained in:
ringabout
2024-01-13 19:00:55 +08:00
committed by GitHub
parent ade5295fd5
commit 8484abc2e4
2 changed files with 26 additions and 5 deletions

View File

@@ -994,12 +994,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

View File

@@ -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()