Skip to content

Commit

Permalink
fixes #24033; Yielding from var fails with pairs and destructuring (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored Sep 2, 2024
1 parent 5e55e16 commit c8af099
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 16 additions & 0 deletions tests/iter/titer_issues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit c8af099

Please sign in to comment.