diff options
author | Neelesh Chandola <neelesh.chandola@outlook.com> | 2019-02-23 18:31:34 +0530 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-02-23 14:01:34 +0100 |
commit | 988412905837271ad8d8502e5337381f8e9624ce (patch) | |
tree | 8c16245d27da9de6f8e932f485108e8ee9493b7f /compiler/transf.nim | |
parent | e89aaaeaab40088eb57a0209e152e90ce16ed51c (diff) | |
download | Nim-988412905837271ad8d8502e5337381f8e9624ce.tar.gz |
Tuple unpacking now works for `for` vars (#10152)
* Tuple unpacking now works for `for` vars * Give error if length of tuple vars != length of tuple * Fix error message showing wrong tuple length * unpacking now works now for mutable items * Update changelog
Diffstat (limited to 'compiler/transf.nim')
-rw-r--r-- | compiler/transf.nim | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/compiler/transf.nim b/compiler/transf.nim index 071cb00ee..a26598094 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -378,9 +378,15 @@ proc transformYield(c: PTransf, n: PNode): PTransNode = for i in countup(0, sonsLen(e) - 1): var v = e.sons[i] if v.kind == nkExprColonExpr: v = v.sons[1] - let lhs = c.transCon.forStmt.sons[i] - let rhs = transform(c, v) - add(result, asgnTo(lhs, rhs)) + if c.transCon.forStmt[i].kind == nkVarTuple: + for j in 0 ..< sonsLen(c.transCon.forStmt[i])-1: + let lhs = c.transCon.forStmt[i][j] + let rhs = transform(c, newTupleAccess(c.graph, v, j)) + add(result, asgnTo(lhs, rhs)) + else: + let lhs = c.transCon.forStmt.sons[i] + let rhs = transform(c, v) + add(result, asgnTo(lhs, rhs)) else: # Unpack the tuple into the loop variables # XXX: BUG: what if `n` is an expression with side-effects? @@ -389,9 +395,15 @@ proc transformYield(c: PTransf, n: PNode): PTransNode = let rhs = transform(c, newTupleAccess(c.graph, e, i)) add(result, asgnTo(lhs, rhs)) else: - let lhs = c.transCon.forStmt.sons[0] - let rhs = transform(c, e) - add(result, asgnTo(lhs, rhs)) + if c.transCon.forStmt.sons[0].kind == nkVarTuple: + for i in 0 ..< sonsLen(c.transCon.forStmt[0])-1: + let lhs = c.transCon.forStmt[0][i] + let rhs = transform(c, newTupleAccess(c.graph, e, i)) + add(result, asgnTo(lhs, rhs)) + else: + let lhs = c.transCon.forStmt.sons[0] + let rhs = transform(c, e) + add(result, asgnTo(lhs, rhs)) inc(c.transCon.yieldStmts) if c.transCon.yieldStmts <= 1: @@ -609,7 +621,11 @@ proc transformFor(c: PTransf, n: PNode): PTransNode = var v = newNodeI(nkVarSection, n.info) for i in countup(0, length - 3): - addVar(v, copyTree(n.sons[i])) # declare new vars + if n[i].kind == nkVarTuple: + for j in 0 ..< sonsLen(n[i])-1: + addVar(v, copyTree(n[i][j])) # declare new vars + else: + addVar(v, copyTree(n.sons[i])) # declare new vars add(stmtList, v.PTransNode) # Bugfix: inlined locals belong to the invoking routine, not to the invoked |