fixes #23180; fixes #19805; prohibits invalid tuple unpacking code in for loop (#23185)

fixes #23180
fixes #19805
This commit is contained in:
ringabout
2024-01-13 21:09:34 +08:00
committed by GitHub
parent 8484abc2e4
commit ab4278d217
2 changed files with 6 additions and 3 deletions

View File

@@ -930,7 +930,10 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode =
if iterAfterVarLent.kind != tyTuple or n.len == 3:
if n.len == 3:
if n[0].kind == nkVarTuple:
if n[0].len-1 != iterAfterVarLent.len:
if iterAfterVarLent.kind != tyTuple:
return localErrorNode(c, n, n[0].info, errTupleUnpackingTupleExpected %
[typeToString(n[1].typ, preferDesc)])
elif n[0].len-1 != iterAfterVarLent.len:
return localErrorNode(c, n, n[0].info, errWrongNumberOfVariables)
for i in 0..<n[0].len-1:

View File

@@ -1,6 +1,6 @@
discard """
cmd: "nim check $options $file"
errormsg: "wrong number of variables"
errormsg: "tuple expected for tuple unpacking, but got 'array[0..2, int]'"
"""
iterator xclusters*[T](a: openArray[T]; s: static[int]): array[s, T] {.inline.} =
@@ -16,4 +16,4 @@ proc m =
for (i, j, k) in xclusters([1, 2, 3, 4, 5], 3):
echo i, j, k
m()
m()