fixes #24033; Yielding from var fails with pairs and destructuring (#24046)

fixes #24033
This commit is contained in:
ringabout
2024-09-03 00:12:48 +08:00
committed by GitHub
parent 5e55e16ad8
commit c8af0996fd
2 changed files with 25 additions and 3 deletions

View File

@@ -410,9 +410,15 @@ proc transformYield(c: PTransf, n: PNode): PNode =
result.add transform(c, v)
for i in 0..<c.transCon.forStmt.len - 2:
let lhs = c.transCon.forStmt[i]
let rhs = transform(c, newTupleAccess(c.graph, tmp, i))
result.add(asgnTo(lhs, rhs))
if c.transCon.forStmt[i].kind == nkVarTuple:
for j in 0..<c.transCon.forStmt[i].len-1:
let lhs = c.transCon.forStmt[i][j]
let rhs = transform(c, newTupleAccess(c.graph, newTupleAccess(c.graph, tmp, i), j))
result.add(asgnTo(lhs, rhs))
else:
let lhs = c.transCon.forStmt[i]
let rhs = transform(c, newTupleAccess(c.graph, tmp, i))
result.add(asgnTo(lhs, rhs))
else:
for i in 0..<c.transCon.forStmt.len - 2:
let lhs = c.transCon.forStmt[i]

View File

@@ -392,3 +392,19 @@ iterator tryFinally() {.closure.} =
var x = tryFinally
x()
block: # bug #24033
type Query = ref object
iterator pairs(query: Query): (int, (string, float32)) =
var output: (int, (string, float32)) = (0, ("foo", 3.14))
for id in @[0, 1, 2]:
output[0] = id
yield output
var collections: seq[(int, string, string)]
for id, (str, num) in Query():
collections.add (id, str, $num)
doAssert collections[1] == (1, "foo", "3.14")