From d855c5442ff259ddde52765af2ba02e31db8f275 Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Wed, 23 Aug 2023 09:56:22 +0300 Subject: [PATCH 1/3] round out tuple unpacking assignment, support underscores fixes #18710 --- compiler/lowerings.nim | 19 ------------------ compiler/semexprs.nim | 33 +++++++++++++++++++++++++++++++- compiler/semstmts.nim | 17 +++++++++------- tests/errmsgs/tassignunpack.nim | 2 +- tests/tuples/ttuples_various.nim | 12 ++++++++++++ 5 files changed, 55 insertions(+), 28 deletions(-) diff --git a/compiler/lowerings.nim b/compiler/lowerings.nim index a083b9195315..42d0f1790c0d 100644 --- a/compiler/lowerings.nim +++ b/compiler/lowerings.nim @@ -122,25 +122,6 @@ proc newTupleAccessRaw*(tup: PNode, i: int): PNode = proc newTryFinally*(body, final: PNode): PNode = result = newTree(nkHiddenTryStmt, body, newTree(nkFinally, final)) -proc lowerTupleUnpackingForAsgn*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: PSym): PNode = - let value = n.lastSon - result = newNodeI(nkStmtList, n.info) - - var temp = newSym(skTemp, getIdent(g.cache, "_"), idgen, owner, value.info, owner.options) - var v = newNodeI(nkLetSection, value.info) - let tempAsNode = newSymNode(temp) #newIdentNode(getIdent(genPrefix & $temp.id), value.info) - - var vpart = newNodeI(nkIdentDefs, tempAsNode.info, 3) - vpart[0] = tempAsNode - vpart[1] = newNodeI(nkTupleClassTy, value.info) - vpart[2] = value - v.add vpart - result.add(v) - - let lhs = n[0] - for i in 0.. but expected 'tuple']# + ^ 'tuple' expected]# diff --git a/tests/tuples/ttuples_various.nim b/tests/tuples/ttuples_various.nim index 97bc70bd286d..e392731d2f0a 100644 --- a/tests/tuples/ttuples_various.nim +++ b/tests/tuples/ttuples_various.nim @@ -197,3 +197,15 @@ block: # bug #22054 var v = A(field: (a: 1314)) doAssert get(v)[0] == 1314 + +block: # tuple unpacking assignment with underscore + var + a = 1 + b = 2 + doAssert (a, b) == (1, 2) + (a, _) = (3, 4) + doAssert (a, b) == (3, 2) + (_, a) = (5, 6) + doAssert (a, b) == (6, 2) + (b, _) = (7, 8) + doAssert (a, b) == (6, 7) From bee77943ff5dfcbf91f80c7a2441e36db39620c9 Mon Sep 17 00:00:00 2001 From: metagn <10591326+metagn@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:51:13 +0300 Subject: [PATCH 2/3] fix test messages --- tests/arc/topt_no_cursor.nim | 8 ++++---- tests/types/tassignemptytuple.nim | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/arc/topt_no_cursor.nim b/tests/arc/topt_no_cursor.nim index 7cfb0a0d50bd..dfb0f0a3867a 100644 --- a/tests/arc/topt_no_cursor.nim +++ b/tests/arc/topt_no_cursor.nim @@ -39,13 +39,13 @@ var lresult lvalue lnext - _ + tmpTupleAsgn lresult = @[123] -_ = ( +tmpTupleAsgn = ( let blitTmp = lresult blitTmp, ";") -lvalue = _[0] -lnext = _[1] +lvalue = tmpTupleAsgn[0] +lnext = tmpTupleAsgn[1] `=sink`(result.value, move lvalue) `=destroy`(lnext) `=destroy_1`(lvalue) diff --git a/tests/types/tassignemptytuple.nim b/tests/types/tassignemptytuple.nim index f3320dec7ace..9d5a311baa32 100644 --- a/tests/types/tassignemptytuple.nim +++ b/tests/types/tassignemptytuple.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "invalid type: 'empty' in this context: '(seq[empty], (seq[empty], set[empty]))' for let" + errormsg: "cannot infer the type of the tuple" file: "tassignemptytuple.nim" line: 11 """ From b195b96274a8dbbb4d9d4b876c95af4b0a5812e1 Mon Sep 17 00:00:00 2001 From: metagn Date: Thu, 24 Aug 2023 03:08:40 +0300 Subject: [PATCH 3/3] use discard instead of continue Co-authored-by: Andreas Rumpf --- compiler/semexprs.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index eb5fdf4e5e1c..cb27ca0ff448 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1855,7 +1855,7 @@ proc makeTupleAssignments(c: PContext; n: PNode): PNode = for i in 0..