round out tuple unpacking assignment, support underscores (#22537)

* round out tuple unpacking assignment, support underscores

fixes #18710

* fix test messages

* use discard instead of continue

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 53d43e9671)
This commit is contained in:
metagn
2023-08-24 07:11:48 +03:00
committed by narimiran
parent c39a0139fc
commit 0d02bee23f
7 changed files with 60 additions and 33 deletions

View File

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

View File

@@ -1,3 +1,3 @@
var a, b = 0
(a, b) = 1 #[tt.Error
^ type mismatch: got <int literal(1)> but expected 'tuple']#
^ 'tuple' expected]#

View File

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

View File

@@ -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
"""